source: 3DVCSoftware/branches/HTM-13.0-MV-draft-2/source/Lib/TLibDecoder/TDecCAVLC.cpp

Last change on this file was 1128, checked in by tech, 10 years ago

Removed 3D-HEVC related code.

  • Property svn:eol-style set to native
File size: 111.8 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
42//! \ingroup TLibDecoder
43//! \{
44
45#if ENC_DEC_TRACE
46
47Void  xTraceSPSHeader (TComSPS *pSPS)
48{
49#if H_MV_ENC_DEC_TRAC
50  if ( g_disableHLSTrace )
51  {
52    return; 
53  }
54  // To avoid mismatches
55  fprintf( g_hTrace, "=========== Sequence Parameter Set LayerId: %d ===========\n", pSPS->getLayerId() );
56#else
57  fprintf( g_hTrace, "=========== Sequence Parameter Set ID: %d ===========\n", pSPS->getSPSId() );
58#endif
59}
60
61Void  xTracePPSHeader (TComPPS *pPPS)
62{
63#if H_MV_ENC_DEC_TRAC
64  if ( g_disableHLSTrace )
65  {
66    return; 
67  }
68  fprintf( g_hTrace, "=========== Picture Parameter Set LayerId: %d ===========\n", pPPS->getLayerId() );
69#else
70  fprintf( g_hTrace, "=========== Picture Parameter Set ID: %d ===========\n", pPPS->getPPSId() );
71#endif
72}
73
74Void  xTraceSliceHeader (TComSlice *pSlice)
75{
76#if H_MV_ENC_DEC_TRAC
77  if ( g_disableHLSTrace )
78  {
79    return; 
80  }
81#endif
82  fprintf( g_hTrace, "=========== Slice ===========\n");
83}
84
85#endif
86
87// ====================================================================================================================
88// Constructor / destructor / create / destroy
89// ====================================================================================================================
90
91TDecCavlc::TDecCavlc()
92{
93}
94
95TDecCavlc::~TDecCavlc()
96{
97}
98
99// ====================================================================================================================
100// Public member functions
101// ====================================================================================================================
102
103void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
104{
105  UInt code;
106  UInt interRPSPred;
107  if (idx > 0)
108  {
109    READ_FLAG(interRPSPred, "inter_ref_pic_set_prediction_flag");  rps->setInterRPSPrediction(interRPSPred);
110  }
111  else
112  {
113    interRPSPred = false;
114    rps->setInterRPSPrediction(false);
115  }
116
117  if (interRPSPred) 
118  {
119    UInt bit;
120    if(idx == sps->getRPSList()->getNumberOfReferencePictureSets())
121    {
122      READ_UVLC(code, "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
123    }
124    else
125    {
126      code = 0;
127    }
128    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
129    Int rIdx =  idx - 1 - code;
130    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
131    TComReferencePictureSet*   rpsRef = sps->getRPSList()->getReferencePictureSet(rIdx);
132    Int k = 0, k0 = 0, k1 = 0;
133    READ_CODE(1, bit, "delta_rps_sign"); // delta_RPS_sign
134    READ_UVLC(code, "abs_delta_rps_minus1");  // absolute delta RPS minus 1
135    Int deltaRPS = (1 - 2 * bit) * (code + 1); // delta_RPS
136    for(Int j=0 ; j <= rpsRef->getNumberOfPictures(); j++)
137    {
138      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
139      Int refIdc = bit;
140      if (refIdc == 0) 
141      {
142        READ_CODE(1, bit, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
143        refIdc = bit<<1; //second bit is "1" if refIdc is 2, "0" if refIdc = 0.
144      }
145      if (refIdc == 1 || refIdc == 2)
146      {
147        Int deltaPOC = deltaRPS + ((j < rpsRef->getNumberOfPictures())? rpsRef->getDeltaPOC(j) : 0);
148        rps->setDeltaPOC(k, deltaPOC);
149        rps->setUsed(k, (refIdc == 1));
150
151        if (deltaPOC < 0)
152        {
153          k0++;
154        }
155        else 
156        {
157          k1++;
158        }
159        k++;
160      } 
161      rps->setRefIdc(j,refIdc); 
162    }
163    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1); 
164    rps->setNumberOfPictures(k);
165    rps->setNumberOfNegativePictures(k0);
166    rps->setNumberOfPositivePictures(k1);
167    rps->sortDeltaPOC();
168  }
169  else
170  {
171    READ_UVLC(code, "num_negative_pics");           rps->setNumberOfNegativePictures(code);
172    READ_UVLC(code, "num_positive_pics");           rps->setNumberOfPositivePictures(code);
173    Int prev = 0;
174    Int poc;
175    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
176    {
177      READ_UVLC(code, "delta_poc_s0_minus1");
178      poc = prev-code-1;
179      prev = poc;
180      rps->setDeltaPOC(j,poc);
181      READ_FLAG(code, "used_by_curr_pic_s0_flag");  rps->setUsed(j,code);
182    }
183    prev = 0;
184    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
185    {
186      READ_UVLC(code, "delta_poc_s1_minus1");
187      poc = prev+code+1;
188      prev = poc;
189      rps->setDeltaPOC(j,poc);
190      READ_FLAG(code, "used_by_curr_pic_s1_flag");  rps->setUsed(j,code);
191    }
192    rps->setNumberOfPictures(rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures());
193  }
194#if PRINT_RPS_INFO
195  rps->printDeltaPOC();
196#endif
197}
198
199Void TDecCavlc::parsePPS(TComPPS* pcPPS)
200{
201#if ENC_DEC_TRACE 
202  xTracePPSHeader (pcPPS);
203#endif
204  UInt  uiCode;
205
206  Int   iCode;
207
208  READ_UVLC( uiCode, "pps_pic_parameter_set_id");
209  assert(uiCode <= 63);
210  pcPPS->setPPSId (uiCode);
211 
212  READ_UVLC( uiCode, "pps_seq_parameter_set_id");
213  assert(uiCode <= 15);
214  pcPPS->setSPSId (uiCode);
215 
216  READ_FLAG( uiCode, "dependent_slice_segments_enabled_flag"    );    pcPPS->setDependentSliceSegmentsEnabledFlag   ( uiCode == 1 );
217  READ_FLAG( uiCode, "output_flag_present_flag" );                    pcPPS->setOutputFlagPresentFlag( uiCode==1 );
218
219  READ_CODE(3, uiCode, "num_extra_slice_header_bits");                pcPPS->setNumExtraSliceHeaderBits(uiCode);
220  READ_FLAG ( uiCode, "sign_data_hiding_flag" ); pcPPS->setSignHideFlag( uiCode );
221
222  READ_FLAG( uiCode,   "cabac_init_present_flag" );            pcPPS->setCabacInitPresentFlag( uiCode ? true : false );
223
224  READ_UVLC(uiCode, "num_ref_idx_l0_default_active_minus1");
225  assert(uiCode <= 14);
226  pcPPS->setNumRefIdxL0DefaultActive(uiCode+1);
227 
228  READ_UVLC(uiCode, "num_ref_idx_l1_default_active_minus1");
229  assert(uiCode <= 14);
230  pcPPS->setNumRefIdxL1DefaultActive(uiCode+1);
231 
232  READ_SVLC(iCode, "init_qp_minus26" );                            pcPPS->setPicInitQPMinus26(iCode);
233  READ_FLAG( uiCode, "constrained_intra_pred_flag" );              pcPPS->setConstrainedIntraPred( uiCode ? true : false );
234  READ_FLAG( uiCode, "transform_skip_enabled_flag" );               
235  pcPPS->setUseTransformSkip ( uiCode ? true : false ); 
236
237  READ_FLAG( uiCode, "cu_qp_delta_enabled_flag" );            pcPPS->setUseDQP( uiCode ? true : false );
238  if( pcPPS->getUseDQP() )
239  {
240    READ_UVLC( uiCode, "diff_cu_qp_delta_depth" );
241    pcPPS->setMaxCuDQPDepth( uiCode );
242  }
243  else
244  {
245    pcPPS->setMaxCuDQPDepth( 0 );
246  }
247  READ_SVLC( iCode, "pps_cb_qp_offset");
248  pcPPS->setChromaCbQpOffset(iCode);
249  assert( pcPPS->getChromaCbQpOffset() >= -12 );
250  assert( pcPPS->getChromaCbQpOffset() <=  12 );
251
252  READ_SVLC( iCode, "pps_cr_qp_offset");
253  pcPPS->setChromaCrQpOffset(iCode);
254  assert( pcPPS->getChromaCrQpOffset() >= -12 );
255  assert( pcPPS->getChromaCrQpOffset() <=  12 );
256
257  READ_FLAG( uiCode, "pps_slice_chroma_qp_offsets_present_flag" );
258  pcPPS->setSliceChromaQpFlag( uiCode ? true : false );
259
260  READ_FLAG( uiCode, "weighted_pred_flag" );          // Use of Weighting Prediction (P_SLICE)
261  pcPPS->setUseWP( uiCode==1 );
262  READ_FLAG( uiCode, "weighted_bipred_flag" );         // Use of Bi-Directional Weighting Prediction (B_SLICE)
263  pcPPS->setWPBiPred( uiCode==1 );
264
265  READ_FLAG( uiCode, "transquant_bypass_enable_flag");
266  pcPPS->setTransquantBypassEnableFlag(uiCode ? true : false);
267  READ_FLAG( uiCode, "tiles_enabled_flag"               );    pcPPS->setTilesEnabledFlag            ( uiCode == 1 );
268  READ_FLAG( uiCode, "entropy_coding_sync_enabled_flag" );    pcPPS->setEntropyCodingSyncEnabledFlag( uiCode == 1 );
269 
270  if( pcPPS->getTilesEnabledFlag() )
271  {
272    READ_UVLC ( uiCode, "num_tile_columns_minus1" );                pcPPS->setNumTileColumnsMinus1( uiCode ); 
273    READ_UVLC ( uiCode, "num_tile_rows_minus1" );                   pcPPS->setNumTileRowsMinus1( uiCode ); 
274    READ_FLAG ( uiCode, "uniform_spacing_flag" );                   pcPPS->setTileUniformSpacingFlag( uiCode == 1 );
275
276    if( !pcPPS->getTileUniformSpacingFlag())
277    {
278      std::vector<Int> columnWidth(pcPPS->getNumTileColumnsMinus1());
279      for(UInt i=0; i<pcPPS->getNumTileColumnsMinus1(); i++)
280      { 
281        READ_UVLC( uiCode, "column_width_minus1" ); 
282        columnWidth[i] = uiCode+1;
283      }
284      pcPPS->setTileColumnWidth(columnWidth);
285
286      std::vector<Int> rowHeight (pcPPS->getTileNumRowsMinus1());
287      for(UInt i=0; i<pcPPS->getTileNumRowsMinus1(); i++)
288      {
289        READ_UVLC( uiCode, "row_height_minus1" );
290        rowHeight[i] = uiCode + 1;
291      }
292      pcPPS->setTileRowHeight(rowHeight);
293    }
294
295    if(pcPPS->getNumTileColumnsMinus1() !=0 || pcPPS->getTileNumRowsMinus1() !=0)
296    {
297      READ_FLAG ( uiCode, "loop_filter_across_tiles_enabled_flag" );   pcPPS->setLoopFilterAcrossTilesEnabledFlag( uiCode ? true : false );
298    }
299  }
300  READ_FLAG( uiCode, "loop_filter_across_slices_enabled_flag" );       pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
301  READ_FLAG( uiCode, "deblocking_filter_control_present_flag" );       pcPPS->setDeblockingFilterControlPresentFlag( uiCode ? true : false );
302  if(pcPPS->getDeblockingFilterControlPresentFlag())
303  {
304    READ_FLAG( uiCode, "deblocking_filter_override_enabled_flag" );    pcPPS->setDeblockingFilterOverrideEnabledFlag( uiCode ? true : false );
305    READ_FLAG( uiCode, "pps_disable_deblocking_filter_flag" );         pcPPS->setPicDisableDeblockingFilterFlag(uiCode ? true : false );
306    if(!pcPPS->getPicDisableDeblockingFilterFlag())
307    {
308      READ_SVLC ( iCode, "pps_beta_offset_div2" );                     pcPPS->setDeblockingFilterBetaOffsetDiv2( iCode );
309      READ_SVLC ( iCode, "pps_tc_offset_div2" );                       pcPPS->setDeblockingFilterTcOffsetDiv2( iCode );
310    }
311  }
312  READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
313  if(pcPPS->getScalingListPresentFlag ())
314  {
315    parseScalingList( pcPPS->getScalingList() );
316  }
317  READ_FLAG( uiCode, "lists_modification_present_flag");
318  pcPPS->setListsModificationPresentFlag(uiCode);
319
320  READ_UVLC( uiCode, "log2_parallel_merge_level_minus2");
321  pcPPS->setLog2ParallelMergeLevelMinus2 (uiCode);
322
323  READ_FLAG( uiCode, "slice_segment_header_extension_present_flag");
324  pcPPS->setSliceHeaderExtensionPresentFlag(uiCode);
325 
326#if H_MV
327  READ_FLAG( uiCode, "pps_extension_present_flag");
328#else
329  READ_FLAG( uiCode, "pps_extension_flag");
330#endif
331  if (uiCode)
332  {
333
334#if H_MV
335    READ_FLAG( uiCode, "pps_range_extensions_flag" ); pcPPS->setPpsRangeExtensionsFlag( uiCode == 1 );
336    READ_FLAG( uiCode, "pps_multilayer_extension_flag" ); pcPPS->setPpsMultilayerExtensionFlag( uiCode == 1 );
337    READ_CODE( 6, uiCode, "pps_extension_6bits" ); pcPPS->setPpsExtension6bits( uiCode );
338    if ( pcPPS->getPpsRangeExtensionsFlag() )
339    { 
340      assert(0); 
341    }
342
343    if ( pcPPS->getPpsMultilayerExtensionFlag() )
344    { 
345      parsePPSMultilayerExtension( pcPPS ); 
346    }
347    if ( pcPPS->getPpsExtension6bits() )
348    {
349
350#endif
351
352      while ( xMoreRbspData() )
353      {
354        READ_FLAG( uiCode, "pps_extension_data_flag");
355      }
356#if H_MV
357    }
358#endif
359  }
360}
361
362
363
364Void  TDecCavlc::parseVUI(TComVUI* pcVUI, TComSPS *pcSPS)
365{
366#if ENC_DEC_TRACE
367  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
368#endif
369  UInt  uiCode;
370
371  READ_FLAG(     uiCode, "aspect_ratio_info_present_flag");           pcVUI->setAspectRatioInfoPresentFlag(uiCode);
372  if (pcVUI->getAspectRatioInfoPresentFlag())
373  {
374    READ_CODE(8, uiCode, "aspect_ratio_idc");                         pcVUI->setAspectRatioIdc(uiCode);
375    if (pcVUI->getAspectRatioIdc() == 255)
376    {
377      READ_CODE(16, uiCode, "sar_width");                             pcVUI->setSarWidth(uiCode);
378      READ_CODE(16, uiCode, "sar_height");                            pcVUI->setSarHeight(uiCode);
379    }
380  }
381
382  READ_FLAG(     uiCode, "overscan_info_present_flag");               pcVUI->setOverscanInfoPresentFlag(uiCode);
383  if (pcVUI->getOverscanInfoPresentFlag())
384  {
385    READ_FLAG(   uiCode, "overscan_appropriate_flag");                pcVUI->setOverscanAppropriateFlag(uiCode);
386  }
387
388  READ_FLAG(     uiCode, "video_signal_type_present_flag");           pcVUI->setVideoSignalTypePresentFlag(uiCode);
389#if H_MV
390  assert( pcSPS->getLayerId() == 0 || !pcVUI->getVideoSignalTypePresentFlag() ); 
391#endif
392  if (pcVUI->getVideoSignalTypePresentFlag())
393  {
394    READ_CODE(3, uiCode, "video_format");                             pcVUI->setVideoFormat(uiCode);
395    READ_FLAG(   uiCode, "video_full_range_flag");                    pcVUI->setVideoFullRangeFlag(uiCode);
396    READ_FLAG(   uiCode, "colour_description_present_flag");          pcVUI->setColourDescriptionPresentFlag(uiCode);
397    if (pcVUI->getColourDescriptionPresentFlag())
398    {
399      READ_CODE(8, uiCode, "colour_primaries");                       pcVUI->setColourPrimaries(uiCode);
400      READ_CODE(8, uiCode, "transfer_characteristics");               pcVUI->setTransferCharacteristics(uiCode);
401      READ_CODE(8, uiCode, "matrix_coefficients");                    pcVUI->setMatrixCoefficients(uiCode);
402    }
403  }
404
405  READ_FLAG(     uiCode, "chroma_loc_info_present_flag");             pcVUI->setChromaLocInfoPresentFlag(uiCode);
406  if (pcVUI->getChromaLocInfoPresentFlag())
407  {
408    READ_UVLC(   uiCode, "chroma_sample_loc_type_top_field" );        pcVUI->setChromaSampleLocTypeTopField(uiCode);
409    READ_UVLC(   uiCode, "chroma_sample_loc_type_bottom_field" );     pcVUI->setChromaSampleLocTypeBottomField(uiCode);
410  }
411
412  READ_FLAG(     uiCode, "neutral_chroma_indication_flag");           pcVUI->setNeutralChromaIndicationFlag(uiCode);
413
414  READ_FLAG(     uiCode, "field_seq_flag");                           pcVUI->setFieldSeqFlag(uiCode);
415
416  READ_FLAG(uiCode, "frame_field_info_present_flag");                 pcVUI->setFrameFieldInfoPresentFlag(uiCode);
417
418  READ_FLAG(     uiCode, "default_display_window_flag");
419  if (uiCode != 0)
420  {
421    Window &defDisp = pcVUI->getDefaultDisplayWindow();
422#if H_MV
423    defDisp.setScaledFlag( false ); 
424    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode );
425    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode );
426    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode );
427    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode );
428#else
429    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
430    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
431    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
432    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
433#endif
434  }
435  TimingInfo *timingInfo = pcVUI->getTimingInfo();
436  READ_FLAG(       uiCode, "vui_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
437  if(timingInfo->getTimingInfoPresentFlag())
438  {
439    READ_CODE( 32, uiCode, "vui_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
440    READ_CODE( 32, uiCode, "vui_time_scale");                       timingInfo->setTimeScale                  (uiCode);
441    READ_FLAG(     uiCode, "vui_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
442    if(timingInfo->getPocProportionalToTimingFlag())
443    {
444      READ_UVLC(   uiCode, "vui_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
445    }
446  READ_FLAG(     uiCode, "hrd_parameters_present_flag");              pcVUI->setHrdParametersPresentFlag(uiCode);
447  if( pcVUI->getHrdParametersPresentFlag() )
448  {
449    parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
450  }
451  }
452  READ_FLAG(     uiCode, "bitstream_restriction_flag");               pcVUI->setBitstreamRestrictionFlag(uiCode);
453  if (pcVUI->getBitstreamRestrictionFlag())
454  {
455    READ_FLAG(   uiCode, "tiles_fixed_structure_flag");               pcVUI->setTilesFixedStructureFlag(uiCode);
456    READ_FLAG(   uiCode, "motion_vectors_over_pic_boundaries_flag");  pcVUI->setMotionVectorsOverPicBoundariesFlag(uiCode);
457    READ_FLAG(   uiCode, "restricted_ref_pic_lists_flag");            pcVUI->setRestrictedRefPicListsFlag(uiCode);
458    READ_UVLC( uiCode, "min_spatial_segmentation_idc");            pcVUI->setMinSpatialSegmentationIdc(uiCode);
459    assert(uiCode < 4096);
460    READ_UVLC(   uiCode, "max_bytes_per_pic_denom" );                 pcVUI->setMaxBytesPerPicDenom(uiCode);
461    READ_UVLC(   uiCode, "max_bits_per_mincu_denom" );                pcVUI->setMaxBitsPerMinCuDenom(uiCode);
462    READ_UVLC(   uiCode, "log2_max_mv_length_horizontal" );           pcVUI->setLog2MaxMvLengthHorizontal(uiCode);
463    READ_UVLC(   uiCode, "log2_max_mv_length_vertical" );             pcVUI->setLog2MaxMvLengthVertical(uiCode);
464  }
465}
466
467Void TDecCavlc::parseHrdParameters(TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1)
468{
469  UInt  uiCode;
470  if( commonInfPresentFlag )
471  {
472    READ_FLAG( uiCode, "nal_hrd_parameters_present_flag" );           hrd->setNalHrdParametersPresentFlag( uiCode == 1 ? true : false );
473    READ_FLAG( uiCode, "vcl_hrd_parameters_present_flag" );           hrd->setVclHrdParametersPresentFlag( uiCode == 1 ? true : false );
474    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
475    {
476      READ_FLAG( uiCode, "sub_pic_cpb_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
477      if( hrd->getSubPicCpbParamsPresentFlag() )
478      {
479        READ_CODE( 8, uiCode, "tick_divisor_minus2" );                hrd->setTickDivisorMinus2( uiCode );
480        READ_CODE( 5, uiCode, "du_cpb_removal_delay_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
481        READ_FLAG( uiCode, "sub_pic_cpb_params_in_pic_timing_sei_flag" ); hrd->setSubPicCpbParamsInPicTimingSEIFlag( uiCode == 1 ? true : false );
482        READ_CODE( 5, uiCode, "dpb_output_delay_du_length_minus1"  ); hrd->setDpbOutputDelayDuLengthMinus1( uiCode );
483      }
484      READ_CODE( 4, uiCode, "bit_rate_scale" );                       hrd->setBitRateScale( uiCode );
485      READ_CODE( 4, uiCode, "cpb_size_scale" );                       hrd->setCpbSizeScale( uiCode );
486      if( hrd->getSubPicCpbParamsPresentFlag() )
487      {
488        READ_CODE( 4, uiCode, "cpb_size_du_scale" );                  hrd->setDuCpbSizeScale( uiCode );
489      }
490      READ_CODE( 5, uiCode, "initial_cpb_removal_delay_length_minus1" ); hrd->setInitialCpbRemovalDelayLengthMinus1( uiCode );
491      READ_CODE( 5, uiCode, "au_cpb_removal_delay_length_minus1" );      hrd->setCpbRemovalDelayLengthMinus1( uiCode );
492      READ_CODE( 5, uiCode, "dpb_output_delay_length_minus1" );       hrd->setDpbOutputDelayLengthMinus1( uiCode );
493    }
494  }
495  Int i, j, nalOrVcl;
496  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
497  {
498    READ_FLAG( uiCode, "fixed_pic_rate_general_flag" );                     hrd->setFixedPicRateFlag( i, uiCode == 1 ? true : false  );
499    if( !hrd->getFixedPicRateFlag( i ) )
500    {
501      READ_FLAG( uiCode, "fixed_pic_rate_within_cvs_flag" );                hrd->setFixedPicRateWithinCvsFlag( i, uiCode == 1 ? true : false  );
502    }
503    else
504    {
505      hrd->setFixedPicRateWithinCvsFlag( i, true );
506    }
507    hrd->setLowDelayHrdFlag( i, 0 ); // Infered to be 0 when not present
508    hrd->setCpbCntMinus1   ( i, 0 ); // Infered to be 0 when not present
509    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
510    {
511      READ_UVLC( uiCode, "elemental_duration_in_tc_minus1" );             hrd->setPicDurationInTcMinus1( i, uiCode );
512    }
513    else
514    {     
515      READ_FLAG( uiCode, "low_delay_hrd_flag" );                      hrd->setLowDelayHrdFlag( i, uiCode == 1 ? true : false  );
516    }
517    if (!hrd->getLowDelayHrdFlag( i ))
518    {
519      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );     
520    }
521    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
522    {
523      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
524          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
525      {
526        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
527        {
528          READ_UVLC( uiCode, "bit_rate_value_minus1" );             hrd->setBitRateValueMinus1( i, j, nalOrVcl, uiCode );
529          READ_UVLC( uiCode, "cpb_size_value_minus1" );             hrd->setCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
530          if( hrd->getSubPicCpbParamsPresentFlag() )
531          {
532            READ_UVLC( uiCode, "cpb_size_du_value_minus1" );       hrd->setDuCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
533            READ_UVLC( uiCode, "bit_rate_du_value_minus1" );       hrd->setDuBitRateValueMinus1( i, j, nalOrVcl, uiCode );
534          }
535          READ_FLAG( uiCode, "cbr_flag" );                          hrd->setCbrFlag( i, j, nalOrVcl, uiCode == 1 ? true : false  );
536        }
537      }
538    }
539  }
540}
541
542Void TDecCavlc::parseSPS(TComSPS* pcSPS)
543{
544#if ENC_DEC_TRACE 
545  xTraceSPSHeader (pcSPS);
546#endif
547
548  UInt  uiCode;
549  READ_CODE( 4,  uiCode, "sps_video_parameter_set_id");          pcSPS->setVPSId        ( uiCode );
550
551#if H_MV
552  if ( pcSPS->getLayerId() == 0 )
553  {
554#endif
555    READ_CODE( 3,  uiCode, "sps_max_sub_layers_minus1" );          pcSPS->setMaxTLayers   ( uiCode+1 );
556    assert(uiCode <= 6);
557#if H_MV
558  }
559  else
560  {
561    READ_CODE( 3, uiCode, "sps_ext_or_max_sub_layers_minus1" ); pcSPS->setSpsExtOrMaxSubLayersMinus1( uiCode );   
562    pcSPS->inferSpsMaxSubLayersMinus1( false, NULL );
563  }
564  if ( !pcSPS->getMultiLayerExtSpsFlag() )
565  {
566#endif
567
568    READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );               pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
569    if ( pcSPS->getMaxTLayers() == 1 )
570    {
571      // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
572      assert( uiCode == 1 );
573    }
574
575    parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
576#if H_MV
577    pcSPS->getPTL()->inferGeneralValues ( true, 0, NULL ); 
578    pcSPS->getPTL()->inferSubLayerValues( pcSPS->getMaxTLayers() - 1, 0, NULL );
579  }
580#endif
581  READ_UVLC(     uiCode, "sps_seq_parameter_set_id" );           pcSPS->setSPSId( uiCode );
582  assert(uiCode <= 15);
583#if H_MV
584  if ( pcSPS->getMultiLayerExtSpsFlag() )
585  {
586    READ_FLAG( uiCode, "update_rep_format_flag" );               pcSPS->setUpdateRepFormatFlag( uiCode == 1 );
587    if ( pcSPS->getUpdateRepFormatFlag() )
588    { 
589      READ_CODE( 8, uiCode, "sps_rep_format_idx" );                pcSPS->setSpsRepFormatIdx( uiCode );
590    }
591  }
592  else
593  {
594#endif
595    READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( uiCode );
596    assert(uiCode <= 3);
597    // 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
598    assert (uiCode == 1);
599    if( uiCode == 3 )
600    {
601      READ_FLAG(     uiCode, "separate_colour_plane_flag");        assert(uiCode == 0);
602    }
603
604    READ_UVLC (    uiCode, "pic_width_in_luma_samples" );          pcSPS->setPicWidthInLumaSamples ( uiCode    );
605    READ_UVLC (    uiCode, "pic_height_in_luma_samples" );         pcSPS->setPicHeightInLumaSamples( uiCode    );
606  READ_FLAG(     uiCode, "conformance_window_flag");
607  if (uiCode != 0)
608  {
609    Window &conf = pcSPS->getConformanceWindow();
610#if H_MV
611    // Needs to be scaled later, when ChromaFormatIdc is known.
612    conf.setScaledFlag( false ); 
613    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode  );
614    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode  );
615    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode  );
616    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode  );   
617  }
618#else
619    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
620    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
621    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
622    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
623#endif
624  }
625
626#if H_MV
627  if ( !pcSPS->getMultiLayerExtSpsFlag() )
628  { 
629#endif
630    READ_UVLC(     uiCode, "bit_depth_luma_minus8" );
631    assert(uiCode <= 6);
632    pcSPS->setBitDepthY( uiCode + 8 );
633    pcSPS->setQpBDOffsetY( (Int) (6*uiCode) );
634
635    READ_UVLC( uiCode,    "bit_depth_chroma_minus8" );
636    assert(uiCode <= 6);
637    pcSPS->setBitDepthC( uiCode + 8 );
638    pcSPS->setQpBDOffsetC( (Int) (6*uiCode) );
639#if H_MV
640  }
641#endif
642
643  READ_UVLC( uiCode,    "log2_max_pic_order_cnt_lsb_minus4" );   pcSPS->setBitsForPOC( 4 + uiCode );
644  assert(uiCode <= 12);
645
646#if H_MV
647  if ( !pcSPS->getMultiLayerExtSpsFlag()) 
648  { 
649#endif
650    UInt subLayerOrderingInfoPresentFlag;
651    READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
652
653    for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
654    {
655      READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
656      pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
657      READ_UVLC ( uiCode, "sps_num_reorder_pics[i]" );
658      pcSPS->setNumReorderPics(uiCode, i);
659      READ_UVLC ( uiCode, "sps_max_latency_increase_plus1[i]");
660      pcSPS->setMaxLatencyIncrease( uiCode, i );
661
662      if (!subLayerOrderingInfoPresentFlag)
663      {
664        for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
665        {
666          pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
667          pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
668          pcSPS->setMaxLatencyIncrease(pcSPS->getMaxLatencyIncrease(0), i);
669        }
670        break;
671      }
672    }
673#if H_MV
674  }
675#endif
676
677  READ_UVLC( uiCode, "log2_min_coding_block_size_minus3" );
678  Int log2MinCUSize = uiCode + 3;
679  pcSPS->setLog2MinCodingBlockSize(log2MinCUSize);
680  READ_UVLC( uiCode, "log2_diff_max_min_coding_block_size" );
681  pcSPS->setLog2DiffMaxMinCodingBlockSize(uiCode);
682
683  if (pcSPS->getPTL()->getGeneralPTL()->getLevelIdc() >= Level::LEVEL5)
684  {
685    assert(log2MinCUSize + pcSPS->getLog2DiffMaxMinCodingBlockSize() >= 5);
686  }
687
688  Int maxCUDepthDelta = uiCode;
689  pcSPS->setMaxCUWidth  ( 1<<(log2MinCUSize + maxCUDepthDelta) ); 
690  pcSPS->setMaxCUHeight ( 1<<(log2MinCUSize + maxCUDepthDelta) );
691  READ_UVLC( uiCode, "log2_min_transform_block_size_minus2" );   pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
692
693  READ_UVLC( uiCode, "log2_diff_max_min_transform_block_size" ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
694  pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) );
695
696  READ_UVLC( uiCode, "max_transform_hierarchy_depth_inter" );    pcSPS->setQuadtreeTUMaxDepthInter( uiCode+1 );
697  READ_UVLC( uiCode, "max_transform_hierarchy_depth_intra" );    pcSPS->setQuadtreeTUMaxDepthIntra( uiCode+1 );
698
699  Int addCuDepth = max (0, log2MinCUSize - (Int)pcSPS->getQuadtreeTULog2MinSize() );
700  pcSPS->setMaxCUDepth( maxCUDepthDelta + addCuDepth ); 
701
702  READ_FLAG( uiCode, "scaling_list_enabled_flag" );                 pcSPS->setScalingListFlag ( uiCode );
703  if(pcSPS->getScalingListFlag())
704  {
705#if H_MV
706    if ( pcSPS->getMultiLayerExtSpsFlag() )
707    {   
708      READ_FLAG( uiCode, "sps_infer_scaling_list_flag" ); pcSPS->setSpsInferScalingListFlag( uiCode == 1 );
709    }
710
711    if ( pcSPS->getSpsInferScalingListFlag() )
712    {
713      READ_CODE( 6, uiCode, "sps_scaling_list_ref_layer_id" ); pcSPS->setSpsScalingListRefLayerId( uiCode );
714    }
715    else
716    {   
717#endif
718      READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
719      if(pcSPS->getScalingListPresentFlag ())
720      {
721        parseScalingList( pcSPS->getScalingList() );
722      }
723#if H_MV
724    }
725#endif
726  }
727  READ_FLAG( uiCode, "amp_enabled_flag" );                          pcSPS->setUseAMP( uiCode );
728  READ_FLAG( uiCode, "sample_adaptive_offset_enabled_flag" );       pcSPS->setUseSAO ( uiCode ? true : false );
729
730  READ_FLAG( uiCode, "pcm_enabled_flag" ); pcSPS->setUsePCM( uiCode ? true : false );
731  if( pcSPS->getUsePCM() )
732  {
733    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_luma_minus1" );          pcSPS->setPCMBitDepthLuma   ( 1 + uiCode );
734    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1" );        pcSPS->setPCMBitDepthChroma ( 1 + uiCode );
735    READ_UVLC( uiCode, "log2_min_pcm_luma_coding_block_size_minus3" );   pcSPS->setPCMLog2MinSize (uiCode+3);
736    READ_UVLC( uiCode, "log2_diff_max_min_pcm_luma_coding_block_size" ); pcSPS->setPCMLog2MaxSize ( uiCode+pcSPS->getPCMLog2MinSize() );
737    READ_FLAG( uiCode, "pcm_loop_filter_disable_flag" );                 pcSPS->setPCMFilterDisableFlag ( uiCode ? true : false );
738  }
739
740  READ_UVLC( uiCode, "num_short_term_ref_pic_sets" );
741  assert(uiCode <= 64);
742  pcSPS->createRPSList(uiCode);
743
744  TComRPSList* rpsList = pcSPS->getRPSList();
745  TComReferencePictureSet* rps;
746
747  for(UInt i=0; i< rpsList->getNumberOfReferencePictureSets(); i++)
748  {
749    rps = rpsList->getReferencePictureSet(i);
750    parseShortTermRefPicSet(pcSPS,rps,i);
751  }
752  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefsPresent(uiCode);
753  if (pcSPS->getLongTermRefsPresent()) 
754  {
755    READ_UVLC( uiCode, "num_long_term_ref_pic_sps" );
756    pcSPS->setNumLongTermRefPicSPS(uiCode);
757    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
758    {
759      READ_CODE( pcSPS->getBitsForPOC(), uiCode, "lt_ref_pic_poc_lsb_sps" );
760      pcSPS->setLtRefPicPocLsbSps(k, uiCode);
761      READ_FLAG( uiCode,  "used_by_curr_pic_lt_sps_flag[i]");
762      pcSPS->setUsedByCurrPicLtSPSFlag(k, uiCode?1:0);
763    }
764  }
765  READ_FLAG( uiCode, "sps_temporal_mvp_enable_flag" );            pcSPS->setTMVPFlagsPresent(uiCode);
766
767  READ_FLAG( uiCode, "sps_strong_intra_smoothing_enable_flag" );  pcSPS->setUseStrongIntraSmoothing(uiCode);
768
769  READ_FLAG( uiCode, "vui_parameters_present_flag" );             pcSPS->setVuiParametersPresentFlag(uiCode);
770
771  if (pcSPS->getVuiParametersPresentFlag())
772  {
773    parseVUI(pcSPS->getVuiParameters(), pcSPS);
774  }
775
776
777#if H_MV
778  READ_FLAG( uiCode, "sps_extension_present_flag");
779  pcSPS->setSpsExtensionPresentFlag( uiCode ); 
780  if (pcSPS->getSpsExtensionPresentFlag( ) )
781#else
782  READ_FLAG( uiCode, "sps_extension_flag");
783  if (uiCode)
784#endif
785  {
786#if H_MV
787    READ_FLAG( uiCode, "sps_range_extensions_flag" ); pcSPS->setSpsRangeExtensionsFlag( uiCode == 1 );
788    READ_FLAG( uiCode, "sps_multilayer_extension_flag" ); pcSPS->setSpsMultilayerExtensionFlag( uiCode == 1 );
789    READ_CODE( 6, uiCode, "sps_extension_6bits" ); pcSPS->setSpsExtension6bits( uiCode );
790  }
791
792  if ( pcSPS->getSpsRangeExtensionsFlag() )
793  {
794    assert( 0 ); 
795  }
796
797  if ( pcSPS->getSpsMultilayerExtensionFlag() )
798  {
799    parseSPSExtension( pcSPS ); 
800  }
801
802  if ( pcSPS->getSpsExtension6bits() )
803  { 
804
805#endif
806    while ( xMoreRbspData() )
807    {
808      READ_FLAG( uiCode, "sps_extension_data_flag");
809    }
810  }
811}
812
813#if H_MV
814Void TDecCavlc::parseSPSExtension( TComSPS* pcSPS )
815{
816  UInt uiCode; 
817  READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );    pcSPS->setInterViewMvVertConstraintFlag(uiCode == 1 ? true : false);
818 
819}
820
821
822Void TDecCavlc::parsePPSMultilayerExtension(TComPPS* pcPPS)
823{
824  UInt uiCode = 0; 
825  READ_FLAG( uiCode, "poc_reset_info_present_flag" ); pcPPS->setPocResetInfoPresentFlag( uiCode == 1 );
826  READ_FLAG( uiCode, "pps_infer_scaling_list_flag" ); pcPPS->setPpsInferScalingListFlag( uiCode == 1 );
827  READ_CODE( 6, uiCode, "pps_scaling_list_ref_layer_id" ); pcPPS->setPpsScalingListRefLayerId( uiCode );
828#if H_MV_HLS_FIX
829
830  UInt numRefLocOffsets;; 
831  READ_UVLC( numRefLocOffsets, "num_ref_loc_offsets" );
832
833  // All of the following stuff is not needed, but allowed to be present.
834  for (Int i = 0; i < numRefLocOffsets; i++ )
835  { 
836    Int   iCode = 0; 
837    READ_CODE( 6, uiCode, "ref_loc_offset_layer_id" ); 
838    READ_FLAG( uiCode, "scaled_ref_layer_offset_present_flag" );
839
840    if (uiCode)
841    {   
842      READ_SVLC( iCode, "scaled_ref_layer_left_offset" );   
843      READ_SVLC( iCode, "scaled_ref_layer_top_offset" );   
844      READ_SVLC( iCode, "scaled_ref_layer_right_offset" ); 
845      READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" ); 
846    }
847
848    READ_FLAG( uiCode, "ref_region_offset_present_flag" ); 
849    if (uiCode)
850    {   
851      READ_SVLC( iCode, "ref_region_left_offset" );     
852      READ_SVLC( iCode, "ref_region_top_offset" );     
853      READ_SVLC( iCode, "ref_region_right_offset" );   
854      READ_SVLC( iCode, "ref_region_bottom_offset" );   
855    }
856
857    READ_FLAG( uiCode, "resample_phase_set_present_flag" );
858    if (uiCode)
859    {     
860      READ_UVLC( uiCode, "phase_hor_luma" );             
861      READ_UVLC( uiCode, "phase_ver_luma" );             
862      READ_UVLC( uiCode, "phase_hor_chroma_plus8" );     
863      READ_UVLC( uiCode, "phase_ver_chroma_plus8" );     
864    }
865  }
866  READ_FLAG( uiCode, "colour_mapping_enabled_flag" );   
867  // This is required to equal to 0 for Multiview Main profile.
868  assert( uiCode == 0 ); 
869#else
870  READ_UVLC( uiCode, "num_ref_loc_offsets" ); assert( uiCode == 0 );
871#endif
872}
873
874#endif
875
876Void TDecCavlc::parseVPS(TComVPS* pcVPS)
877{
878  UInt  uiCode;
879 
880  READ_CODE( 4,  uiCode,  "vps_video_parameter_set_id" );         pcVPS->setVPSId( uiCode );
881#if H_MV
882  READ_FLAG( uiCode, "vps_base_layer_internal_flag" );            pcVPS->setVpsBaseLayerInternalFlag( uiCode == 1 );
883  READ_FLAG( uiCode, "vps_base_layer_available_flag" );           pcVPS->setVpsBaseLayerAvailableFlag( uiCode == 1 );
884#else
885  READ_CODE( 2,  uiCode,  "vps_reserved_three_2bits" );           assert(uiCode == 3);
886#endif
887#if H_MV
888  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayersMinus1( std::min( uiCode, (UInt) ( MAX_NUM_LAYER_IDS-1) )  );
889#else
890  READ_CODE( 6,  uiCode,  "vps_reserved_zero_6bits" );            assert(uiCode == 0);
891#endif
892  READ_CODE( 3,  uiCode,  "vps_max_sub_layers_minus1" );          pcVPS->setMaxTLayers( uiCode + 1 );    assert(uiCode+1 <= MAX_TLAYER);
893  READ_FLAG(     uiCode,  "vps_temporal_id_nesting_flag" );       pcVPS->setTemporalNestingFlag( uiCode ? true:false );
894  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
895
896  READ_CODE( 16, uiCode,  "vps_reserved_ffff_16bits" );           assert(uiCode == 0xffff);
897  parsePTL ( pcVPS->getPTL(), true, pcVPS->getMaxTLayers()-1);
898#if H_MV
899  pcVPS->getPTL()->inferGeneralValues ( true, 0, NULL );
900  pcVPS->getPTL()->inferSubLayerValues( pcVPS->getMaxTLayers() - 1, 0, NULL );
901#endif
902  UInt subLayerOrderingInfoPresentFlag;
903  READ_FLAG(subLayerOrderingInfoPresentFlag, "vps_sub_layer_ordering_info_present_flag");
904  for(UInt i = 0; i <= pcVPS->getMaxTLayers()-1; i++)
905  {
906    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );     pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
907    READ_UVLC( uiCode,  "vps_num_reorder_pics[i]" );          pcVPS->setNumReorderPics( uiCode, i );
908    READ_UVLC( uiCode,  "vps_max_latency_increase_plus1[i]" );      pcVPS->setMaxLatencyIncrease( uiCode, i );
909
910    if (!subLayerOrderingInfoPresentFlag)
911    {
912      for (i++; i <= pcVPS->getMaxTLayers()-1; i++)
913      {
914        pcVPS->setMaxDecPicBuffering(pcVPS->getMaxDecPicBuffering(0), i);
915        pcVPS->setNumReorderPics(pcVPS->getNumReorderPics(0), i);
916        pcVPS->setMaxLatencyIncrease(pcVPS->getMaxLatencyIncrease(0), i);
917      }
918      break;
919    }
920  }
921
922  assert( pcVPS->getNumHrdParameters() < MAX_VPS_OP_SETS_PLUS1 );
923#if H_MV
924  assert( pcVPS->getVpsMaxLayerId() < MAX_VPS_NUH_LAYER_ID_PLUS1 );
925  READ_CODE( 6, uiCode, "vps_max_layer_id" );   pcVPS->setVpsMaxLayerId( uiCode );
926
927  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );  pcVPS->setVpsNumLayerSetsMinus1( uiCode );
928  for( UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx ++ )
929  {
930    for( UInt i = 0; i <= pcVPS->getVpsMaxLayerId(); i ++ )
931#else
932  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
933  READ_CODE( 6, uiCode, "vps_max_nuh_reserved_zero_layer_id" );   pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
934  READ_UVLC(    uiCode, "vps_max_op_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
935  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
936  {
937    // Operation point set
938    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
939#endif
940    {
941      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );     pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
942    }
943  }
944#if H_MV
945  pcVPS->deriveLayerSetLayerIdList(); 
946#endif
947  TimingInfo *timingInfo = pcVPS->getTimingInfo();
948  READ_FLAG(       uiCode, "vps_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
949  if(timingInfo->getTimingInfoPresentFlag())
950  {
951    READ_CODE( 32, uiCode, "vps_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
952    READ_CODE( 32, uiCode, "vps_time_scale");                       timingInfo->setTimeScale                  (uiCode);
953    READ_FLAG(     uiCode, "vps_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
954    if(timingInfo->getPocProportionalToTimingFlag())
955    {
956      READ_UVLC(   uiCode, "vps_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
957    }
958    READ_UVLC( uiCode, "vps_num_hrd_parameters" );                  pcVPS->setNumHrdParameters( uiCode );
959
960    if( pcVPS->getNumHrdParameters() > 0 )
961    {
962      pcVPS->createHrdParamBuffer();
963    }
964    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
965    {
966      READ_UVLC( uiCode, "hrd_op_set_idx" );                       pcVPS->setHrdOpSetIdx( uiCode, i );
967      if( i > 0 )
968      {
969        READ_FLAG( uiCode, "cprms_present_flag[i]" );              pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
970      }
971      else
972      {
973        pcVPS->setCprmsPresentFlag( true, i );
974      }
975
976      parseHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
977    }
978  }
979#if H_MV
980  READ_FLAG( uiCode,  "vps_extension_flag" );                      pcVPS->setVpsExtensionFlag( uiCode == 1 ? true : false );
981  if ( pcVPS->getVpsExtensionFlag() )
982#else
983  READ_FLAG( uiCode,  "vps_extension_flag" );
984  if (uiCode)
985#endif
986  {
987#if H_MV
988    m_pcBitstream->readOutTrailingBits();
989    parseVPSExtension( pcVPS );   
990    READ_FLAG( uiCode,  "vps_extension2_flag" );
991    if (uiCode)
992    {
993#endif 
994        while ( xMoreRbspData() )
995        {
996          READ_FLAG( uiCode, "vps_extension_data_flag");
997        }
998#if H_MV
999    }
1000#endif
1001  }
1002  return; 
1003}
1004
1005#if H_MV
1006Void TDecCavlc::parseVPSExtension( TComVPS* pcVPS )
1007{
1008  UInt uiCode; 
1009
1010  if( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() )
1011  {
1012    parsePTL( pcVPS->getPTL( 1 ),0, pcVPS->getMaxSubLayersMinus1()  ); 
1013   
1014    pcVPS->getPTL( 1 )->inferGeneralValues ( false, 1, pcVPS->getPTL( 0 ) );
1015    pcVPS->getPTL( 1 )->inferSubLayerValues( pcVPS->getMaxSubLayersMinus1(), 1, pcVPS->getPTL( 0 ) );   
1016  }
1017
1018  READ_FLAG( uiCode, "splitting_flag" );                          pcVPS->setSplittingFlag( uiCode == 1 ? true : false );
1019
1020  for( Int sIdx = 0; sIdx < MAX_NUM_SCALABILITY_TYPES; sIdx++ )
1021  {
1022    READ_FLAG( uiCode,  "scalability_mask_flag[i]" );             pcVPS->setScalabilityMaskFlag( sIdx, uiCode == 1 ? true : false );     
1023  }
1024
1025  for( Int sIdx = 0; sIdx < pcVPS->getNumScalabilityTypes( ) - ( pcVPS->getSplittingFlag() ? 1 : 0 ); sIdx++ )
1026  {
1027    READ_CODE( 3, uiCode, "dimension_id_len_minus1[j]" );       pcVPS->setDimensionIdLen( sIdx, uiCode + 1 );
1028  }
1029
1030  if ( pcVPS->getSplittingFlag() )
1031  {
1032    pcVPS->setDimensionIdLen( pcVPS->getNumScalabilityTypes( ) - 1, pcVPS->inferLastDimsionIdLenMinus1() );       
1033  }
1034
1035  READ_FLAG( uiCode, "vps_nuh_layer_id_present_flag" );           pcVPS->setVpsNuhLayerIdPresentFlag( uiCode == 1 ? true : false );
1036
1037  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1038  {
1039    if ( pcVPS->getVpsNuhLayerIdPresentFlag() )
1040    {
1041      READ_CODE( 6, uiCode, "layer_id_in_nuh[i]" );                pcVPS->setLayerIdInNuh( i, uiCode );
1042    }
1043    else
1044    {
1045      pcVPS->setLayerIdInNuh( i, i );; 
1046    }
1047
1048    pcVPS->setLayerIdInVps( pcVPS->getLayerIdInNuh( i ), i ); 
1049
1050    for( Int j = 0; j < pcVPS->getNumScalabilityTypes() ; j++ ) 
1051    {
1052      if ( !pcVPS->getSplittingFlag() )
1053      {
1054        READ_CODE( pcVPS->getDimensionIdLen( j ), uiCode, "dimension_id[i][j]" );  pcVPS->setDimensionId( i, j, uiCode );
1055      }
1056      else
1057      {
1058        pcVPS->setDimensionId( i, j, pcVPS->inferDimensionId( i, j)  );
1059      }
1060    }
1061  }
1062
1063#if H_MV_FIX_NUM_VIEWS
1064  pcVPS->initNumViews(); 
1065#endif
1066
1067  READ_CODE( 4, uiCode, "view_id_len" ); pcVPS->setViewIdLen( uiCode );
1068
1069  if ( pcVPS->getViewIdLen( ) > 0 )
1070  {   
1071    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1072    {
1073      READ_CODE( pcVPS->getViewIdLen( ), uiCode, "view_id_val[i]" ); pcVPS->setViewIdVal( i, uiCode );
1074    }
1075  }
1076  else
1077  {
1078    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1079    {
1080      pcVPS->setViewIdVal( i, 0 ); 
1081    }
1082  }
1083
1084
1085  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1086  {
1087    for( Int j = 0; j < i; j++ )
1088    {
1089      READ_FLAG( uiCode, "direct_dependency_flag[i][j]" );             pcVPS->setDirectDependencyFlag( i, j, uiCode );
1090    }
1091  }
1092  pcVPS->setRefLayers(); 
1093
1094  if ( pcVPS->getNumIndependentLayers() > 1 ) 
1095  {
1096    READ_UVLC( uiCode, "num_add_layer_sets"      ); pcVPS->setNumAddLayerSets( uiCode );
1097  }
1098  for (Int i = 0; i < pcVPS->getNumAddLayerSets(); i++)
1099  {
1100    for (Int j = 1; j < pcVPS->getNumIndependentLayers(); j++)
1101    {
1102      READ_CODE( pcVPS->getHighestLayerIdxPlus1Len( j ) , uiCode, "highest_layer_idx_plus1" ); pcVPS->setHighestLayerIdxPlus1( i, j, uiCode );
1103    }
1104    pcVPS->deriveAddLayerSetLayerIdList( i );
1105  }
1106
1107  READ_FLAG( uiCode, "vps_sub_layers_max_minus1_present_flag" ); pcVPS->setVpsSubLayersMaxMinus1PresentFlag( uiCode == 1 );
1108  if ( pcVPS->getVpsSubLayersMaxMinus1PresentFlag() )
1109  {
1110    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1111    {
1112      READ_CODE( 3, uiCode, "sub_layers_vps_max_minus1" ); pcVPS->setSubLayersVpsMaxMinus1( i, uiCode );   
1113      pcVPS->checkSubLayersVpsMaxMinus1( i ); 
1114    }
1115  } 
1116  else
1117  {
1118    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1119    {
1120      pcVPS->setSubLayersVpsMaxMinus1( i, pcVPS->getMaxTLayers( ) - 1);   
1121    }
1122  }
1123  READ_FLAG( uiCode, "max_tid_ref_present_flag" ); pcVPS->setMaxTidRefPresentFlag( uiCode == 1 );
1124
1125  if ( pcVPS->getMaxTidRefPresentFlag() )
1126  {   
1127    for( Int i = 0; i < pcVPS->getMaxLayersMinus1(); i++ )
1128    {
1129      for( Int j = i + 1; j <= pcVPS->getMaxLayersMinus1(); j++ )
1130      {
1131        if ( pcVPS->getDirectDependencyFlag(j,i) )
1132        {
1133          READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1" ); pcVPS->setMaxTidIlRefPicsPlus1( i, j, uiCode );
1134        }
1135      }
1136    }
1137  }
1138
1139  READ_FLAG( uiCode, "all_ref_layers_active_flag" );             pcVPS->setAllRefLayersActiveFlag( uiCode == 1 );
1140  READ_UVLC( uiCode, "vps_num_profile_tier_level_minus1" );  pcVPS->setVpsNumProfileTierLevelMinus1( uiCode ); 
1141
1142  Int offsetVal =  ( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() ) ? 2 : 1;   
1143  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 2 : 1; i <= pcVPS->getVpsNumProfileTierLevelMinus1(); i++ )
1144  {
1145    READ_FLAG(  uiCode, "vps_profile_present_flag[i]" );    pcVPS->setVpsProfilePresentFlag( i, uiCode == 1 );
1146    parsePTL ( pcVPS->getPTL( offsetVal ), pcVPS->getVpsProfilePresentFlag( i ), pcVPS->getMaxTLayers()-1);
1147    pcVPS->getPTL( offsetVal )->inferGeneralValues ( pcVPS->getVpsProfilePresentFlag( i ), offsetVal, pcVPS->getPTL( offsetVal - 1 ) );   
1148    pcVPS->getPTL( offsetVal )->inferSubLayerValues( pcVPS->getMaxSubLayersMinus1()      , offsetVal, pcVPS->getPTL( offsetVal - 1 ) );   
1149    offsetVal++;
1150  }
1151
1152
1153  if (pcVPS->getNumLayerSets() > 1)
1154  {
1155    READ_UVLC( uiCode, "num_add_olss" ); pcVPS->setNumAddOlss( uiCode );
1156    READ_CODE( 2, uiCode, "default_output_layer_idc" ); pcVPS->setDefaultOutputLayerIdc( std::min( uiCode, (UInt) 2 ) );   
1157  }
1158
1159  pcVPS->initTargetLayerIdLists( ); 
1160
1161
1162  pcVPS->setOutputLayerFlag(0, 0, pcVPS->inferOutputLayerFlag( 0, 0 )); 
1163  pcVPS->setLayerSetIdxForOlsMinus1(0, -1); 
1164
1165  pcVPS->deriveNecessaryLayerFlags( 0 ); 
1166  pcVPS->deriveTargetLayerIdList( 0 ); 
1167
1168  if (pcVPS->getVpsBaseLayerInternalFlag() )
1169  { 
1170    pcVPS->setProfileTierLevelIdx(0,0, pcVPS->inferProfileTierLevelIdx(0,0) );
1171  }
1172  for( Int i = 1; i < pcVPS->getNumOutputLayerSets( ); i++ )
1173  {
1174#if H_MV_HLS_FIX
1175    if( pcVPS->getNumLayerSets() > 2 && i >= pcVPS->getNumLayerSets( ) )   
1176#else
1177    if( i >= pcVPS->getNumLayerSets( ) )   
1178#endif
1179    {       
1180      READ_CODE( pcVPS->getLayerSetIdxForOlsMinus1Len( i ), uiCode, "layer_set_idx_for_ols_minus1[i]" ); pcVPS->setLayerSetIdxForOlsMinus1( i, uiCode ); 
1181    }
1182
1183    if ( i > pcVPS->getVpsNumLayerSetsMinus1() || pcVPS->getDefaultOutputLayerIdc() == 2 )
1184    {       
1185      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1186      {
1187        READ_FLAG( uiCode, "output_layer_flag" ); pcVPS->setOutputLayerFlag( i, j, uiCode == 1 ); 
1188      }
1189    }
1190    else
1191    { 
1192      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1193      {             
1194        pcVPS->setOutputLayerFlag(i,j, pcVPS->inferOutputLayerFlag( i, j )); 
1195      }
1196    }
1197    pcVPS->deriveNecessaryLayerFlags( i ); 
1198    pcVPS->deriveTargetLayerIdList( i ); 
1199
1200    for ( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx(i)); j++ )
1201    {   
1202      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() > 0 )
1203      {
1204        READ_CODE( pcVPS->getProfileTierLevelIdxLen(), uiCode,"profile_tier_level_idx[ i ][ j ]" );   pcVPS->setProfileTierLevelIdx( i, j, uiCode ); 
1205      }
1206      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() == 0 )
1207      {
1208        pcVPS->setProfileTierLevelIdx( i , j, pcVPS->inferProfileTierLevelIdx( i, j) );
1209      }
1210    }
1211
1212    if( pcVPS->getNumOutputLayersInOutputLayerSet( i ) == 1 && pcVPS->getNumDirectRefLayers( pcVPS->getOlsHighestOutputLayerId( i ) ) > 0 )
1213    {
1214      READ_FLAG( uiCode, "alt_output_layer_flag[ i ]" ); pcVPS->setAltOutputLayerFlag( i, uiCode == 1 );
1215    }
1216  }
1217
1218  READ_UVLC( uiCode, "vps_num_rep_formats_minus1" ); pcVPS->setVpsNumRepFormatsMinus1( uiCode );
1219
1220  for (Int i = 0; i <= pcVPS->getVpsNumRepFormatsMinus1(); i++ )
1221  { 
1222    assert( pcVPS->getRepFormat(i) == NULL ); 
1223    TComRepFormat* curRepFormat = new TComRepFormat(); 
1224    TComRepFormat* prevRepFormat = i > 0 ? pcVPS->getRepFormat( i - 1) : NULL; 
1225    parseRepFormat( i, curRepFormat ,  prevRepFormat); 
1226    pcVPS->setRepFormat(i, curRepFormat ); 
1227  }
1228
1229  if ( pcVPS->getVpsNumRepFormatsMinus1() > 0 )
1230  {
1231    READ_FLAG( uiCode, "rep_format_idx_present_flag" ); pcVPS->setRepFormatIdxPresentFlag( uiCode == 1 );
1232  }
1233  if( pcVPS->getRepFormatIdxPresentFlag() ) 
1234  {
1235    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1236    {
1237        READ_CODE( pcVPS->getVpsRepFormatIdxLen(), uiCode, "vps_rep_format_idx[i]" ); pcVPS->setVpsRepFormatIdx( i, uiCode );
1238    }
1239  }
1240  else
1241  {
1242    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1243    {
1244      pcVPS->setVpsRepFormatIdx( i, pcVPS->inferVpsRepFormatIdx( i ) );
1245    }
1246  }
1247
1248  READ_FLAG( uiCode, "max_one_active_ref_layer_flag" ); pcVPS->setMaxOneActiveRefLayerFlag ( uiCode == 1 ); 
1249
1250#if H_MV_HLS7_GEN || H_MV_HLS_FIX
1251  READ_FLAG( uiCode, "vps_poc_lsb_aligned_flag" ); pcVPS->setVpsPocLsbAlignedFlag( uiCode == 1 );
1252#endif
1253  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1254  {
1255    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) )  ==  0 )
1256    {     
1257      READ_FLAG( uiCode, "poc_lsb_not_present_flag" ); pcVPS->setPocLsbNotPresentFlag( i, uiCode == 1 );
1258    }
1259  }
1260
1261  parseDpbSize( pcVPS ); 
1262
1263  READ_UVLC( uiCode, "direct_dep_type_len_minus2")    ; pcVPS->setDirectDepTypeLenMinus2   ( uiCode ); 
1264
1265  READ_FLAG( uiCode, "default_direct_dependency_flag" ); pcVPS->setDefaultDirectDependencyFlag( uiCode == 1 );
1266  if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1267  { 
1268    READ_CODE( pcVPS->getDirectDepTypeLenMinus2( ) + 2, uiCode, "default_direct_dependency_type" ); pcVPS->setDefaultDirectDependencyType( uiCode );
1269  }
1270
1271  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ?  1 : 2; i <= pcVPS->getMaxLayersMinus1(); i++ )
1272  {
1273    for( Int j = pcVPS->getVpsBaseLayerInternalFlag() ?  0 : 1; j < i; j++ )
1274    {
1275      if (pcVPS->getDirectDependencyFlag( i, j) )
1276      {       
1277        if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1278        { 
1279          pcVPS->setDirectDependencyType( i, j , pcVPS->getDefaultDirectDependencyType( ) );
1280        }
1281        else
1282        {
1283
1284          READ_CODE( pcVPS->getDirectDepTypeLenMinus2( ) + 2,  uiCode, "direct_dependency_type[i][j]" ); pcVPS->setDirectDependencyType( i, j , uiCode);
1285        }
1286      }
1287    }
1288  } 
1289  READ_UVLC( uiCode, "vps_non_vui_extension_length" ); pcVPS->setVpsNonVuiExtensionLength( uiCode ); 
1290  for ( Int i = 1; i <= pcVPS->getVpsNonVuiExtensionLength(); i++ )
1291  {
1292    READ_CODE( 8, uiCode, "vps_non_vui_extension_data_byte" );
1293  }
1294  READ_FLAG( uiCode, "vps_vui_present_flag" );  pcVPS->setVpsVuiPresentFlag( uiCode == 1 );
1295  if( pcVPS->getVpsVuiPresentFlag() )
1296  {
1297    m_pcBitstream->readOutTrailingBits(); // vps_vui_alignment_bit_equal_to_one
1298    parseVPSVUI( pcVPS ); 
1299  }     
1300#if H_MV_HLS_FIX
1301  else
1302#endif
1303  {
1304    TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1305    assert( pcVPSVUI ); 
1306    pcVPSVUI->inferVpsVui( false ); 
1307  }
1308  pcVPS->checkVPSExtensionSyntax(); 
1309}
1310
1311Void TDecCavlc::parseRepFormat( Int i, TComRepFormat* pcRepFormat, TComRepFormat* pcPrevRepFormat )
1312{
1313  assert( pcRepFormat ); 
1314
1315  UInt uiCode; 
1316
1317  READ_CODE( 16, uiCode, "pic_width_vps_in_luma_samples" );  pcRepFormat->setPicWidthVpsInLumaSamples ( uiCode );
1318  READ_CODE( 16, uiCode, "pic_height_vps_in_luma_samples" ); pcRepFormat->setPicHeightVpsInLumaSamples( uiCode );
1319  READ_FLAG( uiCode, "chroma_and_bit_depth_vps_present_flag" ); pcRepFormat->setChromaAndBitDepthVpsPresentFlag( uiCode == 1 );
1320
1321  pcRepFormat->checkChromaAndBitDepthVpsPresentFlag( i ); 
1322
1323  if ( pcRepFormat->getChromaAndBitDepthVpsPresentFlag() )
1324  { 
1325    READ_CODE( 2,  uiCode, "chroma_format_vps_idc" );          pcRepFormat->setChromaFormatVpsIdc       ( uiCode );
1326    if ( pcRepFormat->getChromaFormatVpsIdc() == 3 )
1327    {
1328      READ_FLAG( uiCode, "separate_colour_plane_vps_flag" ); pcRepFormat->setSeparateColourPlaneVpsFlag( uiCode == 1 ); 
1329    }
1330    READ_CODE( 4,  uiCode, "bit_depth_vps_luma_minus8" );      pcRepFormat->setBitDepthVpsLumaMinus8    ( uiCode );
1331    READ_CODE( 4,  uiCode, "bit_depth_vps_chroma_minus8" );    pcRepFormat->setBitDepthVpsChromaMinus8  ( uiCode );
1332  }
1333  else
1334  {
1335    pcRepFormat->inferChromaAndBitDepth(pcPrevRepFormat, false ); 
1336  }
1337  READ_FLAG( uiCode, "conformance_window_vps_flag" ); pcRepFormat->setConformanceWindowVpsFlag( uiCode == 1 );
1338  if ( pcRepFormat->getConformanceWindowVpsFlag() )
1339  {
1340    READ_UVLC( uiCode, "conf_win_vps_left_offset" ); pcRepFormat->setConfWinVpsLeftOffset( uiCode );
1341    READ_UVLC( uiCode, "conf_win_vps_right_offset" ); pcRepFormat->setConfWinVpsRightOffset( uiCode );
1342    READ_UVLC( uiCode, "conf_win_vps_top_offset" ); pcRepFormat->setConfWinVpsTopOffset( uiCode );
1343    READ_UVLC( uiCode, "conf_win_vps_bottom_offset" ); pcRepFormat->setConfWinVpsBottomOffset( uiCode );
1344  }
1345}
1346
1347
1348Void TDecCavlc::parseVPSVUI( TComVPS* pcVPS )
1349{
1350  assert( pcVPS ); 
1351
1352  TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1353
1354  assert( pcVPSVUI ); 
1355
1356  UInt uiCode; 
1357  READ_FLAG( uiCode, "cross_layer_pic_type_aligned_flag" ); pcVPSVUI->setCrossLayerPicTypeAlignedFlag( uiCode == 1 );
1358  if ( !pcVPSVUI->getCrossLayerPicTypeAlignedFlag() )
1359  { 
1360    READ_FLAG( uiCode, "cross_layer_irap_aligned_flag" ); pcVPSVUI->setCrossLayerIrapAlignedFlag( uiCode == 1 );
1361  }
1362  if( pcVPSVUI->getCrossLayerIrapAlignedFlag( ) )
1363  {
1364    READ_FLAG( uiCode, "all_layers_idr_aligned_flag" ); pcVPSVUI->setAllLayersIdrAlignedFlag( uiCode == 1 );
1365  }
1366  READ_FLAG( uiCode, "bit_rate_present_vps_flag" ); pcVPSVUI->setBitRatePresentVpsFlag( uiCode == 1 );
1367  READ_FLAG( uiCode, "pic_rate_present_vps_flag" ); pcVPSVUI->setPicRatePresentVpsFlag( uiCode == 1 );
1368  if( pcVPSVUI->getBitRatePresentVpsFlag( )  ||  pcVPSVUI->getPicRatePresentVpsFlag( ) )
1369  {
1370    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <  pcVPS->getNumLayerSets(); i++ )
1371    {
1372      for( Int j = 0; j  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( i ); j++ ) 
1373      {
1374        if( pcVPSVUI->getBitRatePresentVpsFlag( ) )
1375        {
1376          READ_FLAG( uiCode, "bit_rate_present_flag" ); pcVPSVUI->setBitRatePresentFlag( i, j, uiCode == 1 );           
1377        }
1378        if( pcVPSVUI->getPicRatePresentVpsFlag( )  )
1379        {
1380          READ_FLAG( uiCode, "pic_rate_present_flag" ); pcVPSVUI->setPicRatePresentFlag( i, j, uiCode == 1 );
1381        }
1382        if( pcVPSVUI->getBitRatePresentFlag( i, j ) )
1383        {
1384          READ_CODE( 16, uiCode, "avg_bit_rate" ); pcVPSVUI->setAvgBitRate( i, j, uiCode );
1385          READ_CODE( 16, uiCode, "max_bit_rate" ); pcVPSVUI->setMaxBitRate( i, j, uiCode );
1386        }
1387        if( pcVPSVUI->getPicRatePresentFlag( i, j ) )
1388        {
1389          READ_CODE( 2,  uiCode, "constant_pic_rate_idc" ); pcVPSVUI->setConstantPicRateIdc( i, j, uiCode );
1390          READ_CODE( 16, uiCode, "avg_pic_rate" );          pcVPSVUI->setAvgPicRate( i, j, uiCode );
1391        }
1392      }
1393    }
1394  }
1395
1396  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); pcVPSVUI->setVideoSignalInfoIdxPresentFlag( uiCode == 1 );
1397  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() )
1398  {
1399    READ_CODE( 4, uiCode, "vps_num_video_signal_info_minus1" ); pcVPSVUI->setVpsNumVideoSignalInfoMinus1( uiCode );
1400  }
1401  else
1402  {
1403    pcVPSVUI->setVpsNumVideoSignalInfoMinus1( pcVPS->getMaxLayersMinus1() - pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1 ); 
1404  }
1405
1406  for( Int i = 0; i <= pcVPSVUI->getVpsNumVideoSignalInfoMinus1(); i++ )
1407  {
1408    assert( pcVPSVUI->getVideoSignalInfo( i ) == NULL ); 
1409    TComVideoSignalInfo* curVideoSignalInfo = new TComVideoSignalInfo();     
1410    parseVideoSignalInfo( curVideoSignalInfo ); 
1411    pcVPSVUI->setVideoSignalInfo(i, curVideoSignalInfo ); 
1412  }
1413
1414  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() && pcVPSVUI->getVpsNumVideoSignalInfoMinus1() > 0 )
1415  {
1416    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1417    {
1418      READ_CODE( 4, uiCode, "vps_video_signal_info_idx" ); pcVPSVUI->setVpsVideoSignalInfoIdx( i, uiCode );
1419    }
1420  }
1421  else if ( !pcVPSVUI->getVideoSignalInfoIdxPresentFlag() )
1422  {
1423    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1424    {
1425      pcVPSVUI->setVpsVideoSignalInfoIdx( i, i );
1426    }
1427  }
1428  else
1429  {
1430    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1431    {
1432      pcVPSVUI->setVpsVideoSignalInfoIdx( i, 0 );
1433    }
1434  }
1435
1436  READ_FLAG( uiCode, "tiles_not_in_use_flag" ); pcVPSVUI->setTilesNotInUseFlag( uiCode == 1 );
1437  if( !pcVPSVUI->getTilesNotInUseFlag() ) 
1438  {     
1439    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1440    {
1441      READ_FLAG( uiCode, "tiles_in_use_flag[i]" ); pcVPSVUI->setTilesInUseFlag( i, uiCode == 1 );
1442      if( pcVPSVUI->getTilesInUseFlag( i ) ) 
1443      {
1444        READ_FLAG( uiCode, "loop_filter_not_across_tiles_flag[i]" ); pcVPSVUI->setLoopFilterNotAcrossTilesFlag( i, uiCode == 1 );
1445      }
1446    } 
1447    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 2; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1448    {
1449      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ) ; j++ )
1450      {
1451        Int layerIdx = pcVPS->getLayerIdInVps(pcVPS->getIdDirectRefLayer(pcVPS->getLayerIdInNuh( i ) , j  )); 
1452        if( pcVPSVUI->getTilesInUseFlag( i )  &&  pcVPSVUI->getTilesInUseFlag( layerIdx ) ) 
1453        {
1454          READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); pcVPSVUI->setTileBoundariesAlignedFlag( i, j, uiCode == 1 );
1455        }
1456      } 
1457    }
1458  } 
1459 
1460  READ_FLAG( uiCode, "wpp_not_in_use_flag" ); pcVPSVUI->setWppNotInUseFlag( uiCode == 1 );
1461 
1462  if( !pcVPSVUI->getWppNotInUseFlag( ))
1463  {
1464    for( Int i = 0; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1465    {
1466      READ_FLAG( uiCode, "wpp_in_use_flag[i]" ); pcVPSVUI->setWppInUseFlag( i, uiCode == 1 );
1467    }
1468  }
1469  READ_FLAG( uiCode, "single_layer_for_non_irap_flag" ); pcVPSVUI->setSingleLayerForNonIrapFlag( uiCode == 1 );
1470  READ_FLAG( uiCode, "higher_layer_irap_skip_flag" ); pcVPSVUI->setHigherLayerIrapSkipFlag( uiCode == 1 );
1471  READ_FLAG( uiCode, "ilp_restricted_ref_layers_flag" ); pcVPSVUI->setIlpRestrictedRefLayersFlag( uiCode == 1 );
1472
1473  if( pcVPSVUI->getIlpRestrictedRefLayersFlag( ) )
1474  {
1475    for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1476    {
1477      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ); j++ )
1478      {
1479        if( pcVPS->getVpsBaseLayerInternalFlag() || pcVPS->getIdDirectRefLayer( pcVPS->getLayerIdInNuh( i ), j ) > 0 )
1480        {       
1481          READ_UVLC( uiCode, "min_spatial_segment_offset_plus1" ); pcVPSVUI->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
1482          if( pcVPSVUI->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
1483          {
1484            READ_FLAG( uiCode, "ctu_based_offset_enabled_flag" ); pcVPSVUI->setCtuBasedOffsetEnabledFlag( i, j, uiCode == 1 );
1485            if( pcVPSVUI->getCtuBasedOffsetEnabledFlag( i, j ) )
1486            {
1487              READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1" ); pcVPSVUI->setMinHorizontalCtuOffsetPlus1( i, j, uiCode );
1488            }
1489          }
1490        }
1491      }
1492    }
1493  }
1494
1495  READ_FLAG( uiCode, "vps_vui_bsp_hrd_present_flag" ); pcVPSVUI->setVpsVuiBspHrdPresentFlag( uiCode == 1 );
1496  if ( pcVPSVUI->getVpsVuiBspHrdPresentFlag( ) )
1497  {
1498    assert(pcVPS->getTimingInfo()->getTimingInfoPresentFlag() == 1);
1499    parseVpsVuiBspHrdParameters( pcVPS ); 
1500  }
1501  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1502  {
1503    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i )) == 0 ) 
1504    {
1505      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); pcVPSVUI->setBaseLayerParameterSetCompatibilityFlag( i, uiCode == 1 );
1506    }
1507  }
1508}
1509
1510Void TDecCavlc::parseVpsVuiBspHrdParameters( TComVPS* pcVPS )
1511{
1512  assert( pcVPS ); 
1513
1514  TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1515
1516  assert( pcVPSVUI ); 
1517
1518  TComVpsVuiBspHrdParameters*  vpsVuiBspHrdP = pcVPSVUI->getVpsVuiBspHrdParameters(); 
1519  assert( vpsVuiBspHrdP == NULL ); 
1520  vpsVuiBspHrdP = new TComVpsVuiBspHrdParameters; 
1521  pcVPSVUI->setVpsVuiBspHrdParameters( vpsVuiBspHrdP ); 
1522  UInt uiCode; 
1523  READ_UVLC( uiCode, "vps_num_add_hrd_params" ); vpsVuiBspHrdP->setVpsNumAddHrdParams( uiCode );
1524  vpsVuiBspHrdP->createAfterVpsNumAddHrdParams( pcVPS ); 
1525  for( Int i = pcVPS->getNumHrdParameters(); i < pcVPS->getNumHrdParameters() + vpsVuiBspHrdP->getVpsNumAddHrdParams(); i++ )
1526  { 
1527    if( i > 0 ) 
1528    {
1529      READ_FLAG( uiCode, "cprms_add_present_flag" ); vpsVuiBspHrdP->setCprmsAddPresentFlag( i, uiCode == 1 );
1530    }
1531    else
1532    {
1533       vpsVuiBspHrdP->setCprmsAddPresentFlag( i, true );
1534    }
1535
1536    READ_UVLC( uiCode, "num_sub_layer_hrd_minus1" ); vpsVuiBspHrdP->setNumSubLayerHrdMinus1( i, uiCode );
1537    TComHRD* hrdParameters = vpsVuiBspHrdP->getHrdParametermeters( i ); 
1538    parseHrdParameters( hrdParameters, vpsVuiBspHrdP->getCprmsAddPresentFlag( i ), vpsVuiBspHrdP->getNumSubLayerHrdMinus1( i ) );     
1539  }
1540
1541  vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( 0, 0, 0);
1542  vpsVuiBspHrdP->createAfterNumPartitionsInSchemeMinus1( 0, 0 );
1543
1544  for( Int h = 0; h < pcVPS->getNumOutputLayerSets(); h++ )
1545  { 
1546    if ( h == 0)
1547    {
1548      vpsVuiBspHrdP->setNumSignalledPartitioningSchemes( h, 0 );
1549    }
1550    else
1551    {
1552      READ_UVLC( uiCode, "num_signalled_partitioning_schemes" ); vpsVuiBspHrdP->setNumSignalledPartitioningSchemes( h, uiCode );
1553    }   
1554    vpsVuiBspHrdP->createAfterNumSignalledPartitioningSchemes( h );
1555
1556    for( Int j = 0; j < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; j++ )
1557    { 
1558      if ( j == 0 && h == 0 )
1559      {
1560        vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( h, j, uiCode );
1561      }
1562      else if( j == 0 )
1563      {
1564        vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( h, j, pcVPS->getNumLayersInIdList( h ) - 1 );
1565      }
1566      else
1567      {
1568        READ_UVLC( uiCode, "num_partitions_in_scheme_minus1" ); vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( h, j, uiCode );
1569      }
1570      vpsVuiBspHrdP->createAfterNumPartitionsInSchemeMinus1( h, j );
1571
1572      for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ ) 
1573      {
1574        for( Int r = 0; r < pcVPS->getNumLayersInIdList(pcVPS->olsIdxToLsIdx( h ) )   ; r++ ) 
1575        {
1576          if( h == 0 && j == 0 && k == 0 && r == 0 )
1577          {
1578             vpsVuiBspHrdP->setLayerIncludedInPartitionFlag( h, j, k, r, true );
1579          }
1580          else if ( h > 0 && j == 0 )
1581          {
1582             vpsVuiBspHrdP->setLayerIncludedInPartitionFlag( h, j, k, r, (k == r) );
1583          }
1584          else
1585          {
1586            READ_FLAG( uiCode, "layer_included_in_partition_flag" ); vpsVuiBspHrdP->setLayerIncludedInPartitionFlag( h, j, k, r, uiCode == 1 );
1587          }         
1588        }
1589      }
1590    } 
1591    if ( h > 0 )
1592    {
1593      for( Int i = 0; i < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; i++ ) 
1594      {
1595        for( Int t = 0; t  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( pcVPS->olsIdxToLsIdx( i ) ); t++ )
1596        { 
1597          READ_UVLC( uiCode, "num_bsp_schedules_minus1" ); vpsVuiBspHrdP->setNumBspSchedulesMinus1( h, i, t, uiCode );
1598          vpsVuiBspHrdP->createAfterNumBspSchedulesMinus1( h, i, t );
1599          for( Int j = 0; j  <=  vpsVuiBspHrdP->getNumBspSchedulesMinus1( h, i, t ); j++ ) 
1600          {
1601            for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ )
1602            { 
1603              READ_CODE( vpsVuiBspHrdP->getBspHrdIdxLen( pcVPS ), uiCode, "bsp_hrd_idx" ); vpsVuiBspHrdP->setBspHrdIdx( h, i, t, j, k, uiCode );
1604              READ_UVLC( uiCode, "bsp_sched_idx" ); vpsVuiBspHrdP->setBspSchedIdx( h, i, t, j, k, uiCode );
1605            } 
1606          }
1607        } 
1608      }
1609    }
1610  } 
1611}
1612
1613Void TDecCavlc::parseVideoSignalInfo( TComVideoSignalInfo* pcVideoSignalInfo ) 
1614{
1615  UInt uiCode; 
1616  READ_CODE( 3, uiCode, "video_vps_format" );             pcVideoSignalInfo->setVideoVpsFormat( uiCode );
1617  READ_FLAG( uiCode, "video_full_range_vps_flag" );       pcVideoSignalInfo->setVideoFullRangeVpsFlag( uiCode == 1 );
1618  READ_CODE( 8, uiCode, "colour_primaries_vps" );         pcVideoSignalInfo->setColourPrimariesVps( uiCode );
1619  READ_CODE( 8, uiCode, "transfer_characteristics_vps" ); pcVideoSignalInfo->setTransferCharacteristicsVps( uiCode );
1620  READ_CODE( 8, uiCode, "matrix_coeffs_vps" );            pcVideoSignalInfo->setMatrixCoeffsVps( uiCode );
1621}
1622
1623Void TDecCavlc::parseDpbSize( TComVPS* vps )
1624{
1625  UInt uiCode; 
1626  TComDpbSize* dpbSize = vps->getDpbSize(); 
1627  assert ( dpbSize != 0 ); 
1628
1629  for( Int i = 1; i < vps->getNumOutputLayerSets(); i++ )
1630  { 
1631    Int currLsIdx = vps->olsIdxToLsIdx( i ); 
1632    READ_FLAG( uiCode, "sub_layer_flag_info_present_flag" ); dpbSize->setSubLayerFlagInfoPresentFlag( i, uiCode == 1 );
1633    for( Int j = 0; j  <=  vps->getMaxSubLayersInLayerSetMinus1( currLsIdx ); j++ )
1634    { 
1635      if( j > 0  &&  dpbSize->getSubLayerDpbInfoPresentFlag( i, j )  ) 
1636      {
1637        READ_FLAG( uiCode, "sub_layer_dpb_info_present_flag" ); dpbSize->setSubLayerDpbInfoPresentFlag( i, j, uiCode == 1 );
1638      }
1639      if( dpbSize->getSubLayerDpbInfoPresentFlag( i, j ) )
1640      { 
1641        for( Int k = 0; k < vps->getNumLayersInIdList( currLsIdx ); k++ )   
1642        {
1643          if ( vps->getNecessaryLayerFlag( i, k ) && ( vps->getVpsBaseLayerInternalFlag() || ( vps->getLayerSetLayerIdList(vps->olsIdxToLsIdx(i),k) != 0 ) ))
1644          {
1645            READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1" ); dpbSize->setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
1646          }
1647          else
1648          {
1649            if ( vps->getNecessaryLayerFlag( i, k ) && ( j == 0 ) && ( k == 0 ))
1650            {
1651              dpbSize->setMaxVpsDecPicBufferingMinus1(i ,k, j, 0 );
1652            }
1653          }
1654        }
1655        READ_UVLC( uiCode, "max_vps_num_reorder_pics" ); dpbSize->setMaxVpsNumReorderPics( i, j, uiCode );
1656        READ_UVLC( uiCode, "max_vps_latency_increase_plus1" ); dpbSize->setMaxVpsLatencyIncreasePlus1( i, j, uiCode );
1657      }
1658      else
1659      {
1660        if ( j > 0 )
1661        {
1662          for( Int k = 0; k < vps->getNumLayersInIdList( vps->olsIdxToLsIdx( i ) ); k++ )   
1663          {
1664            if ( vps->getNecessaryLayerFlag(i, k ) )
1665            {           
1666              dpbSize->setMaxVpsDecPicBufferingMinus1( i, k, j, dpbSize->getMaxVpsDecPicBufferingMinus1( i,k, j - 1 ) );
1667            }
1668          }
1669          dpbSize->setMaxVpsNumReorderPics      ( i, j, dpbSize->getMaxVpsNumReorderPics      ( i, j - 1 ) );
1670          dpbSize->setMaxVpsLatencyIncreasePlus1( i, j, dpbSize->getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
1671        }
1672      }
1673    } 
1674  } 
1675}
1676
1677#endif
1678#if H_MV
1679Void TDecCavlc::parseSliceHeader (TComSlice*& rpcSlice, ParameterSetManagerDecoder *parameterSetManager, Int targetOlsIdx)
1680#else
1681Void TDecCavlc::parseSliceHeader (TComSlice*& rpcSlice, ParameterSetManagerDecoder *parameterSetManager)
1682#endif
1683{
1684  UInt  uiCode;
1685  Int   iCode;
1686
1687#if ENC_DEC_TRACE
1688  xTraceSliceHeader(rpcSlice);
1689#endif
1690  TComPPS* pps = NULL;
1691  TComSPS* sps = NULL;
1692#if H_MV
1693  TComVPS* vps = NULL;
1694#endif
1695
1696  UInt firstSliceSegmentInPic;
1697  READ_FLAG( firstSliceSegmentInPic, "first_slice_segment_in_pic_flag" );
1698  if( rpcSlice->getRapPicFlag())
1699  { 
1700    READ_FLAG( uiCode, "no_output_of_prior_pics_flag" );  //ignored -- updated already
1701#if SETTING_NO_OUT_PIC_PRIOR
1702    rpcSlice->setNoOutputPriorPicsFlag(uiCode ? true : false);
1703#else
1704    rpcSlice->setNoOutputPicPrior( false );
1705#endif
1706  }
1707
1708  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  rpcSlice->setPPSId(uiCode);
1709  pps = parameterSetManager->getPrefetchedPPS(uiCode);
1710  //!KS: need to add error handling code here, if PPS is not available
1711  assert(pps!=0);
1712  sps = parameterSetManager->getPrefetchedSPS(pps->getSPSId());
1713  //!KS: need to add error handling code here, if SPS is not available
1714  assert(sps!=0);
1715#if H_MV
1716  vps = parameterSetManager->getPrefetchedVPS(sps->getVPSId());
1717  assert( vps != NULL );
1718 
1719  sps->inferRepFormat  ( vps , rpcSlice->getLayerId() ); 
1720  sps->inferScalingList( parameterSetManager->getActiveSPS( sps->getSpsScalingListRefLayerId() ) );   
1721  sps->inferSpsMaxDecPicBufferingMinus1( vps, targetOlsIdx, rpcSlice->getLayerId(), false ); 
1722
1723  if ( sps->getVuiParametersPresentFlag() )
1724  {
1725    sps->getVuiParameters()->inferVideoSignalInfo( vps, rpcSlice->getLayerId() ); 
1726  }
1727  rpcSlice->setVPS(vps);     
1728  rpcSlice->setViewId   ( vps->getViewId   ( rpcSlice->getLayerId() )      );
1729  rpcSlice->setViewIndex( vps->getViewIndex( rpcSlice->getLayerId() )      ); 
1730#endif
1731  rpcSlice->setSPS(sps);
1732  rpcSlice->setPPS(pps);
1733  if( pps->getDependentSliceSegmentsEnabledFlag() && ( !firstSliceSegmentInPic ))
1734  {
1735    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       rpcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
1736  }
1737  else
1738  {
1739    rpcSlice->setDependentSliceSegmentFlag(false);
1740  }
1741  Int numCTUs = ((sps->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((sps->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
1742  Int maxParts = (1<<(sps->getMaxCUDepth()<<1));
1743  UInt sliceSegmentAddress = 0;
1744  Int bitsSliceSegmentAddress = 0;
1745  while(numCTUs>(1<<bitsSliceSegmentAddress))
1746  {
1747    bitsSliceSegmentAddress++;
1748  }
1749
1750  if(!firstSliceSegmentInPic)
1751  {
1752    READ_CODE( bitsSliceSegmentAddress, sliceSegmentAddress, "slice_segment_address" );
1753  }
1754  //set uiCode to equal slice start address (or dependent slice start address)
1755  Int startCuAddress = maxParts*sliceSegmentAddress;
1756  rpcSlice->setSliceSegmentCurStartCUAddr( startCuAddress );
1757  rpcSlice->setSliceSegmentCurEndCUAddr(numCTUs*maxParts);
1758
1759  if (rpcSlice->getDependentSliceSegmentFlag())
1760  {
1761    rpcSlice->setNextSlice          ( false );
1762    rpcSlice->setNextSliceSegment ( true  );
1763  }
1764  else
1765  {
1766    rpcSlice->setNextSlice          ( true  );
1767    rpcSlice->setNextSliceSegment ( false );
1768
1769    rpcSlice->setSliceCurStartCUAddr(startCuAddress);
1770    rpcSlice->setSliceCurEndCUAddr(numCTUs*maxParts);
1771  }
1772 
1773#if H_MV
1774    UInt slicePicOrderCntLsb = 0;
1775#endif
1776
1777  if(!rpcSlice->getDependentSliceSegmentFlag())
1778  {
1779#if H_MV   
1780    Int esb = 0; //Don't use i, otherwise will shadow something below
1781
1782    if ( rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
1783    {
1784      esb++; 
1785      READ_FLAG( uiCode, "discardable_flag" ); rpcSlice->setDiscardableFlag( uiCode == 1 );
1786      if ( uiCode == 1 )
1787      {
1788        assert(rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
1789          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
1790          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
1791          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
1792          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
1793      }
1794    }
1795
1796    if ( rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
1797    {
1798      esb++; 
1799      READ_FLAG( uiCode, "cross_layer_bla_flag" ); rpcSlice->setCrossLayerBlaFlag( uiCode == 1 );
1800    }
1801    rpcSlice->checkCrossLayerBlaFlag( ); 
1802
1803#if !H_MV_HLS7_GEN && !H_MV_HLS_FIX
1804    if ( rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
1805    {
1806      esb++; 
1807      READ_FLAG( uiCode, "poc_reset_flag" ); rpcSlice->setPocResetFlag( uiCode == 1 );
1808    }
1809#endif
1810
1811    for (; esb < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); esb++)   
1812#else
1813    for (Int i = 0; i < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
1814#endif     
1815    {
1816      READ_FLAG(uiCode, "slice_reserved_undetermined_flag[]"); // ignored
1817    }
1818
1819    READ_UVLC (    uiCode, "slice_type" );            rpcSlice->setSliceType((SliceType)uiCode);
1820    if( pps->getOutputFlagPresentFlag() )
1821    {
1822      READ_FLAG( uiCode, "pic_output_flag" );    rpcSlice->setPicOutputFlag( uiCode ? true : false );
1823    }
1824    else
1825    {
1826      rpcSlice->setPicOutputFlag( true );
1827    }
1828    // in the first version chroma_format_idc is equal to one, thus colour_plane_id will not be present
1829    assert (sps->getChromaFormatIdc() == 1 );
1830    // if( separate_colour_plane_flag  ==  1 )
1831    //   colour_plane_id                                      u(2)
1832
1833
1834#if H_MV
1835    Int iPOClsb = slicePicOrderCntLsb;  // Needed later
1836    if ( (rpcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( rpcSlice->getLayerIdInVps())) || !rpcSlice->getIdrPicFlag() )
1837    {
1838      READ_CODE(sps->getBitsForPOC(), slicePicOrderCntLsb, "slice_pic_order_cnt_lsb");       
1839    }   
1840    rpcSlice->setSlicePicOrderCntLsb( slicePicOrderCntLsb ); 
1841
1842    Bool picOrderCntMSBZeroFlag = false;     
1843
1844    // as in HM code. However are all cases for IRAP picture with NoRaslOutputFlag equal to 1 covered??
1845    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ); 
1846    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ); 
1847    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ); 
1848    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag ||   rpcSlice->getIdrPicFlag(); 
1849
1850    // TBD picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getLayerId() > 0 &&   !rpcSlice->getFirstPicInLayerDecodedFlag() );
1851
1852    Int picOrderCntMSB = 0; 
1853
1854    if ( !picOrderCntMSBZeroFlag )
1855    {
1856      Int prevPicOrderCnt    = rpcSlice->getPrevTid0POC();
1857      Int maxPicOrderCntLsb  = 1 << sps->getBitsForPOC();
1858      Int prevPicOrderCntLsb = prevPicOrderCnt & (maxPicOrderCntLsb - 1);
1859      Int prevPicOrderCntMsb = prevPicOrderCnt - prevPicOrderCntLsb;
1860           
1861      if( ( slicePicOrderCntLsb  <  prevPicOrderCntLsb ) && ( ( prevPicOrderCntLsb - slicePicOrderCntLsb )  >=  ( maxPicOrderCntLsb / 2 ) ) )
1862      {
1863        picOrderCntMSB = prevPicOrderCntMsb + maxPicOrderCntLsb;
1864      }
1865      else if( (slicePicOrderCntLsb  >  prevPicOrderCntLsb )  && ( (slicePicOrderCntLsb - prevPicOrderCntLsb )  >  ( maxPicOrderCntLsb / 2 ) ) ) 
1866      {
1867        picOrderCntMSB = prevPicOrderCntMsb - maxPicOrderCntLsb;
1868      }
1869      else
1870      {
1871        picOrderCntMSB = prevPicOrderCntMsb;
1872      }   
1873    }
1874     
1875    rpcSlice->setPOC( picOrderCntMSB + slicePicOrderCntLsb );
1876    if ( rpcSlice->getPocResetFlag() ) 
1877    {
1878      rpcSlice->setPocBeforeReset   ( rpcSlice->getPOC() ); 
1879      rpcSlice->setPOC              ( 0 );
1880    }     
1881#endif
1882
1883    if( rpcSlice->getIdrPicFlag() )
1884    {
1885#if !H_MV
1886      rpcSlice->setPOC(0);
1887#endif
1888      TComReferencePictureSet* rps = rpcSlice->getLocalRPS();
1889      rps->setNumberOfNegativePictures(0);
1890      rps->setNumberOfPositivePictures(0);
1891      rps->setNumberOfLongtermPictures(0);
1892      rps->setNumberOfPictures(0);
1893      rpcSlice->setRPS(rps);
1894#if H_MV
1895      rpcSlice->setEnableTMVPFlag(false);
1896#endif
1897    }
1898    else
1899    {
1900#if !H_MV
1901      READ_CODE(sps->getBitsForPOC(), uiCode, "pic_order_cnt_lsb"); 
1902      Int iPOClsb = uiCode;
1903      Int iPrevPOC = rpcSlice->getPrevTid0POC();
1904      Int iMaxPOClsb = 1<< sps->getBitsForPOC();
1905      Int iPrevPOClsb = iPrevPOC & (iMaxPOClsb - 1);
1906      Int iPrevPOCmsb = iPrevPOC-iPrevPOClsb;
1907      Int iPOCmsb;
1908      if( ( iPOClsb  <  iPrevPOClsb ) && ( ( iPrevPOClsb - iPOClsb )  >=  ( iMaxPOClsb / 2 ) ) )
1909      {
1910        iPOCmsb = iPrevPOCmsb + iMaxPOClsb;
1911      }
1912      else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) ) 
1913      {
1914        iPOCmsb = iPrevPOCmsb - iMaxPOClsb;
1915      }
1916      else
1917      {
1918        iPOCmsb = iPrevPOCmsb;
1919      }
1920      if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1921        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1922        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
1923      {
1924        // For BLA picture types, POCmsb is set to 0.
1925        iPOCmsb = 0;
1926      }
1927      rpcSlice->setPOC              (iPOCmsb+iPOClsb);
1928#endif
1929      TComReferencePictureSet* rps;
1930      rps = rpcSlice->getLocalRPS();
1931      rpcSlice->setRPS(rps);
1932      READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
1933      if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
1934      {       
1935        parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
1936#if H_MV
1937        if ( !rps->getInterRPSPrediction( ) )
1938        { // check sum of num_positive_pics and num_negative_pics
1939          rps->checkMaxNumPics( 
1940            vps->getVpsExtensionFlag(), 
1941            MAX_INT,  // To be replaced by MaxDbpSize
1942            rpcSlice->getLayerId(), 
1943            sps->getMaxDecPicBuffering( sps->getSpsMaxSubLayersMinus1() ) - 1 );
1944        }
1945#endif
1946      }
1947      else // use reference to short-term reference picture set in PPS
1948      {
1949        Int numBits = 0;
1950        while ((1 << numBits) < rpcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
1951        {
1952          numBits++;
1953        }
1954        if (numBits > 0)
1955        {
1956          READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
1957        }
1958        else
1959        {
1960          uiCode = 0;
1961        }
1962        *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
1963      }
1964      if(sps->getLongTermRefsPresent())
1965      {
1966        Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
1967        UInt numOfLtrp = 0;
1968        UInt numLtrpInSPS = 0;
1969        if (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
1970        {
1971          READ_UVLC( uiCode, "num_long_term_sps");
1972          numLtrpInSPS = uiCode;
1973          numOfLtrp += numLtrpInSPS;
1974          rps->setNumberOfLongtermPictures(numOfLtrp);
1975        }
1976        Int bitsForLtrpInSPS = 0;
1977        while (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
1978        {
1979          bitsForLtrpInSPS++;
1980        }
1981        READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
1982        numOfLtrp += uiCode;
1983        rps->setNumberOfLongtermPictures(numOfLtrp);
1984        Int maxPicOrderCntLSB = 1 << rpcSlice->getSPS()->getBitsForPOC();
1985        Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;;
1986        for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
1987        {
1988          Int pocLsbLt;
1989          if (k < numLtrpInSPS)
1990          {
1991            uiCode = 0;
1992            if (bitsForLtrpInSPS > 0)
1993            {
1994              READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
1995            }
1996            Int usedByCurrFromSPS=rpcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(uiCode);
1997
1998            pocLsbLt = rpcSlice->getSPS()->getLtRefPicPocLsbSps(uiCode);
1999            rps->setUsed(j,usedByCurrFromSPS);
2000          }
2001          else
2002          {
2003            READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
2004            READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
2005          }
2006          READ_FLAG(uiCode,"delta_poc_msb_present_flag");
2007          Bool mSBPresentFlag = uiCode ? true : false;
2008          if(mSBPresentFlag)                 
2009          {
2010            READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
2011            Bool deltaFlag = false;
2012            //            First LTRP                               || First LTRP from SH
2013            if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
2014            {
2015              deltaFlag = true;
2016            }
2017            if(deltaFlag)
2018            {
2019              deltaPocMSBCycleLT = uiCode;
2020            }
2021            else
2022            {
2023              deltaPocMSBCycleLT = uiCode + prevDeltaMSB;             
2024            }
2025
2026            Int pocLTCurr = rpcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
2027                                        - iPOClsb + pocLsbLt;
2028            rps->setPOC     (j, pocLTCurr); 
2029            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLTCurr);
2030            rps->setCheckLTMSBPresent(j,true); 
2031          }
2032          else
2033          {
2034            rps->setPOC     (j, pocLsbLt);
2035            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLsbLt);
2036            rps->setCheckLTMSBPresent(j,false); 
2037           
2038            // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
2039            if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
2040            {
2041              deltaPocMSBCycleLT = 0;
2042            }
2043          }
2044          prevDeltaMSB = deltaPocMSBCycleLT;
2045        }
2046        offset += rps->getNumberOfLongtermPictures();
2047        rps->setNumberOfPictures(offset);       
2048      } 
2049#if H_MV
2050      if ( !rps->getInterRPSPrediction( ) )
2051      { // check sum of NumPositivePics, NumNegativePics, num_long_term_sps and num_long_term_pics
2052        rps->checkMaxNumPics( 
2053          vps->getVpsExtensionFlag(), 
2054            MAX_INT,  // To be replaced by MaxDbpsize
2055          rpcSlice->getLayerId(), 
2056          sps->getMaxDecPicBuffering( sps->getSpsMaxSubLayersMinus1() ) - 1 );
2057      }
2058#endif
2059      if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2060        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
2061        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
2062      {
2063        // In the case of BLA picture types, rps data is read from slice header but ignored
2064        rps = rpcSlice->getLocalRPS();
2065        rps->setNumberOfNegativePictures(0);
2066        rps->setNumberOfPositivePictures(0);
2067        rps->setNumberOfLongtermPictures(0);
2068        rps->setNumberOfPictures(0);
2069        rpcSlice->setRPS(rps);
2070      }
2071      if (rpcSlice->getSPS()->getTMVPFlagsPresent())
2072      {
2073#if H_MV
2074        READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" );
2075#else
2076        READ_FLAG( uiCode, "slice_temporal_mvp_enable_flag" );
2077#endif
2078        rpcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false ); 
2079      }
2080      else
2081      {
2082        rpcSlice->setEnableTMVPFlag(false);
2083      }
2084    }
2085#if H_MV
2086    Bool interLayerPredLayerIdcPresentFlag = false; 
2087    Int layerId       = rpcSlice->getLayerId(); 
2088    if( rpcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumDirectRefLayers( layerId ) > 0 )
2089    {   
2090      READ_FLAG( uiCode, "inter_layer_pred_enabled_flag" ); rpcSlice->setInterLayerPredEnabledFlag( uiCode == 1 );
2091      if( rpcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerId ) > 1 )
2092      {           
2093        if( !vps->getMaxOneActiveRefLayerFlag()) 
2094        {
2095          READ_CODE( rpcSlice->getNumInterLayerRefPicsMinus1Len( ), uiCode, "num_inter_layer_ref_pics_minus1" ); rpcSlice->setNumInterLayerRefPicsMinus1( uiCode );
2096        }
2097        if ( rpcSlice->getNumActiveRefLayerPics() != vps->getNumDirectRefLayers( layerId ) )
2098        {
2099          interLayerPredLayerIdcPresentFlag = true; 
2100          for( Int idx = 0; idx < rpcSlice->getNumActiveRefLayerPics(); idx++ )   
2101          {
2102            READ_CODE( rpcSlice->getInterLayerPredLayerIdcLen( ), uiCode, "inter_layer_pred_layer_idc" ); rpcSlice->setInterLayerPredLayerIdc( idx, uiCode );
2103          }
2104        }
2105      } 
2106    }
2107    if ( !interLayerPredLayerIdcPresentFlag )
2108    {
2109      for( Int i = 0; i < rpcSlice->getNumActiveRefLayerPics(); i++ )   
2110      {
2111        rpcSlice->setInterLayerPredLayerIdc( i, rpcSlice->getRefLayerPicIdc( i ) );
2112      }
2113    }
2114#endif
2115    if(sps->getUseSAO())
2116    {
2117      READ_FLAG(uiCode, "slice_sao_luma_flag");  rpcSlice->setSaoEnabledFlag((Bool)uiCode);
2118      READ_FLAG(uiCode, "slice_sao_chroma_flag");  rpcSlice->setSaoEnabledFlagChroma((Bool)uiCode);
2119    }
2120
2121    if (rpcSlice->getIdrPicFlag())
2122    {
2123      rpcSlice->setEnableTMVPFlag(false);
2124    }
2125    if (!rpcSlice->isIntra())
2126    {
2127
2128      READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
2129      if (uiCode)
2130      {
2131        READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
2132        if (rpcSlice->isInterB())
2133        {
2134          READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
2135        }
2136        else
2137        {
2138          rpcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
2139        }
2140      }
2141      else
2142      {
2143        rpcSlice->setNumRefIdx(REF_PIC_LIST_0, rpcSlice->getPPS()->getNumRefIdxL0DefaultActive());
2144        if (rpcSlice->isInterB())
2145        {
2146          rpcSlice->setNumRefIdx(REF_PIC_LIST_1, rpcSlice->getPPS()->getNumRefIdxL1DefaultActive());
2147        }
2148        else
2149        {
2150          rpcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
2151        }
2152      }
2153    }
2154    // }
2155    TComRefPicListModification* refPicListModification = rpcSlice->getRefPicListModification();
2156    if(!rpcSlice->isIntra())
2157    {
2158      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
2159      {
2160        refPicListModification->setRefPicListModificationFlagL0( 0 );
2161      }
2162      else
2163      {
2164        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
2165      }
2166
2167      if(refPicListModification->getRefPicListModificationFlagL0())
2168      { 
2169        uiCode = 0;
2170        Int i = 0;
2171        Int numRpsCurrTempList0 = rpcSlice->getNumRpsCurrTempList();
2172        if ( numRpsCurrTempList0 > 1 )
2173        {
2174          Int length = 1;
2175          numRpsCurrTempList0 --;
2176          while ( numRpsCurrTempList0 >>= 1) 
2177          {
2178            length ++;
2179          }
2180          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
2181          {
2182            READ_CODE( length, uiCode, "list_entry_l0" );
2183            refPicListModification->setRefPicSetIdxL0(i, uiCode );
2184          }
2185        }
2186        else
2187        {
2188          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
2189          {
2190            refPicListModification->setRefPicSetIdxL0(i, 0 );
2191          }
2192        }
2193      }
2194    }
2195    else
2196    {
2197      refPicListModification->setRefPicListModificationFlagL0(0);
2198    }
2199    if(rpcSlice->isInterB())
2200    {
2201      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
2202      {
2203        refPicListModification->setRefPicListModificationFlagL1( 0 );
2204      }
2205      else
2206      {
2207        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
2208      }
2209      if(refPicListModification->getRefPicListModificationFlagL1())
2210      {
2211        uiCode = 0;
2212        Int i = 0;
2213        Int numRpsCurrTempList1 = rpcSlice->getNumRpsCurrTempList();
2214        if ( numRpsCurrTempList1 > 1 )
2215        {
2216          Int length = 1;
2217          numRpsCurrTempList1 --;
2218          while ( numRpsCurrTempList1 >>= 1) 
2219          {
2220            length ++;
2221          }
2222          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
2223          {
2224            READ_CODE( length, uiCode, "list_entry_l1" );
2225            refPicListModification->setRefPicSetIdxL1(i, uiCode );
2226          }
2227        }
2228        else
2229        {
2230          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
2231          {
2232            refPicListModification->setRefPicSetIdxL1(i, 0 );
2233          }
2234        }
2235      }
2236    } 
2237    else
2238    {
2239      refPicListModification->setRefPicListModificationFlagL1(0);
2240    }
2241    if (rpcSlice->isInterB())
2242    {
2243      READ_FLAG( uiCode, "mvd_l1_zero_flag" );       rpcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
2244    }
2245
2246    rpcSlice->setCabacInitFlag( false ); // default
2247    if(pps->getCabacInitPresentFlag() && !rpcSlice->isIntra())
2248    {
2249      READ_FLAG(uiCode, "cabac_init_flag");
2250      rpcSlice->setCabacInitFlag( uiCode ? true : false );
2251    }
2252
2253    if ( rpcSlice->getEnableTMVPFlag() )
2254    {
2255      if ( rpcSlice->getSliceType() == B_SLICE )
2256      {
2257        READ_FLAG( uiCode, "collocated_from_l0_flag" );
2258        rpcSlice->setColFromL0Flag(uiCode);
2259      }
2260      else
2261      {
2262        rpcSlice->setColFromL0Flag( 1 );
2263      }
2264
2265      if ( rpcSlice->getSliceType() != I_SLICE &&
2266          ((rpcSlice->getColFromL0Flag() == 1 && rpcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
2267           (rpcSlice->getColFromL0Flag() == 0 && rpcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
2268      {
2269        READ_UVLC( uiCode, "collocated_ref_idx" );
2270        rpcSlice->setColRefIdx(uiCode);
2271      }
2272      else
2273      {
2274        rpcSlice->setColRefIdx(0);
2275      }
2276    }
2277    if ( (pps->getUseWP() && rpcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && rpcSlice->getSliceType()==B_SLICE) )
2278    {
2279      xParsePredWeightTable(rpcSlice);
2280      rpcSlice->initWpScaling();
2281    }
2282    if (!rpcSlice->isIntra())
2283    {
2284      READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
2285      rpcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
2286    }
2287
2288    READ_SVLC( iCode, "slice_qp_delta" );
2289    rpcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
2290
2291    assert( rpcSlice->getSliceQp() >= -sps->getQpBDOffsetY() );
2292    assert( rpcSlice->getSliceQp() <=  51 );
2293
2294    if (rpcSlice->getPPS()->getSliceChromaQpFlag())
2295    {
2296      READ_SVLC( iCode, "slice_qp_delta_cb" );
2297      rpcSlice->setSliceQpDeltaCb( iCode );
2298      assert( rpcSlice->getSliceQpDeltaCb() >= -12 );
2299      assert( rpcSlice->getSliceQpDeltaCb() <=  12 );
2300      assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) >= -12 );
2301      assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) <=  12 );
2302
2303      READ_SVLC( iCode, "slice_qp_delta_cr" );
2304      rpcSlice->setSliceQpDeltaCr( iCode );
2305      assert( rpcSlice->getSliceQpDeltaCr() >= -12 );
2306      assert( rpcSlice->getSliceQpDeltaCr() <=  12 );
2307      assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) >= -12 );
2308      assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) <=  12 );
2309    }
2310
2311    if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
2312    {
2313      if(rpcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag())
2314      {
2315        READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        rpcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
2316      }
2317      else
2318      { 
2319        rpcSlice->setDeblockingFilterOverrideFlag(0);
2320      }
2321      if(rpcSlice->getDeblockingFilterOverrideFlag())
2322      {
2323        READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   rpcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
2324        if(!rpcSlice->getDeblockingFilterDisable())
2325        {
2326          READ_SVLC( iCode, "slice_beta_offset_div2" );                       rpcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
2327          assert(rpcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
2328                 rpcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
2329          READ_SVLC( iCode, "slice_tc_offset_div2" );                         rpcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
2330          assert(rpcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
2331                 rpcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
2332        }
2333      }
2334      else
2335      {
2336        rpcSlice->setDeblockingFilterDisable   ( rpcSlice->getPPS()->getPicDisableDeblockingFilterFlag() );
2337        rpcSlice->setDeblockingFilterBetaOffsetDiv2( rpcSlice->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
2338        rpcSlice->setDeblockingFilterTcOffsetDiv2  ( rpcSlice->getPPS()->getDeblockingFilterTcOffsetDiv2() );
2339      }
2340    }
2341    else
2342    { 
2343      rpcSlice->setDeblockingFilterDisable       ( false );
2344      rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
2345      rpcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
2346    }
2347
2348    Bool isSAOEnabled = (!rpcSlice->getSPS()->getUseSAO())?(false):(rpcSlice->getSaoEnabledFlag()||rpcSlice->getSaoEnabledFlagChroma());
2349    Bool isDBFEnabled = (!rpcSlice->getDeblockingFilterDisable());
2350
2351    if(rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
2352    {
2353      READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
2354    }
2355    else
2356    {
2357      uiCode = rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
2358    }
2359    rpcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
2360
2361  }
2362 
2363    UInt *entryPointOffset          = NULL;
2364    UInt numEntryPointOffsets, offsetLenMinus1;
2365  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
2366  {
2367    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets"); rpcSlice->setNumEntryPointOffsets ( numEntryPointOffsets );
2368    if (numEntryPointOffsets>0)
2369    {
2370      READ_UVLC(offsetLenMinus1, "offset_len_minus1");
2371    }
2372    entryPointOffset = new UInt[numEntryPointOffsets];
2373    for (UInt idx=0; idx<numEntryPointOffsets; idx++)
2374    {
2375      READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
2376      entryPointOffset[ idx ] = uiCode + 1;
2377    }
2378  }
2379  else
2380  {
2381    rpcSlice->setNumEntryPointOffsets ( 0 );
2382  }
2383
2384
2385  if(pps->getSliceHeaderExtensionPresentFlag())
2386  {
2387#if H_MV
2388    READ_UVLC( uiCode, "slice_segment_header_extension_length" ); rpcSlice->setSliceSegmentHeaderExtensionLength( uiCode );
2389    UInt posFollSliceSegHeaderExtLen = m_pcBitstream->getNumBitsRead();
2390
2391    if( rpcSlice->getPPS()->getPocResetInfoPresentFlag() )
2392    {
2393      READ_CODE( 2, uiCode, "poc_reset_idc" ); rpcSlice->setPocResetIdc( uiCode );
2394    }
2395    else
2396    {
2397      rpcSlice->setPocResetIdc( 0 );
2398    }
2399    rpcSlice->checkPocResetIdc(); 
2400
2401    if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && slicePicOrderCntLsb > 0 )
2402    {
2403      assert( rpcSlice->getPocResetIdc() != 2 );
2404    }
2405
2406    if( rpcSlice->getPocResetIdc() !=  0 )
2407    {
2408      READ_CODE( 6, uiCode, "poc_reset_period_id" ); rpcSlice->setPocResetPeriodId( uiCode );
2409    }
2410    else
2411    {
2412      // TODO Copy poc_reset_period from earlier picture
2413      rpcSlice->setPocResetPeriodId( 0 );
2414    }
2415   
2416    if( rpcSlice->getPocResetIdc() ==  3 ) 
2417    {
2418      READ_FLAG( uiCode, "full_poc_reset_flag" ); rpcSlice->setFullPocResetFlag( uiCode == 1 );
2419      READ_CODE( rpcSlice->getPocLsbValLen() , uiCode, "poc_lsb_val" ); rpcSlice->setPocLsbVal( uiCode );
2420    }         
2421    rpcSlice->checkPocLsbVal(); 
2422
2423    // Derive the value of PocMs8bValRequiredFlag
2424#if !H_MV_HLS_FIX
2425    rpcSlice->setPocMsbValRequiredFlag( rpcSlice->getCraPicFlag() || rpcSlice->getBlaPicFlag()
2426                                          /* || TODO related to vps_poc_lsb_aligned_flag */
2427                                          );
2428#endif
2429
2430#if H_MV_HLS_FIX
2431    if( !rpcSlice->getPocMsbValRequiredFlag() && rpcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
2432#else
2433    if( !rpcSlice->getPocMsbValRequiredFlag() /* TODO &&  rpcSlice->getVPS()->getVpsPocLsbAlignedFlag() */ )
2434#endif
2435    {
2436      READ_FLAG( uiCode, "poc_msb_val_present_flag" ); rpcSlice->setPocMsbValPresentFlag( uiCode == 1 );
2437    }
2438    else
2439    {
2440      rpcSlice->setPocMsbValPresentFlag( rpcSlice->inferPocMsbValPresentFlag( ) ); 
2441    }
2442
2443   
2444    if( rpcSlice->getPocMsbValPresentFlag() )
2445    {
2446      READ_UVLC( uiCode, "poc_msb_val" ); rpcSlice->setPocMsbVal( uiCode );
2447    }
2448
2449    while( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) < rpcSlice->getSliceSegmentHeaderExtensionLength() * 8 )
2450    {
2451     READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
2452    }
2453    assert( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) == rpcSlice->getSliceSegmentHeaderExtensionLength() * 8  ); 
2454  }
2455#else
2456    READ_UVLC( uiCode, "slice_header_extension_length" );
2457    for(Int i=0; i<uiCode; i++)
2458    {
2459      UInt ignore;
2460      READ_CODE(8,ignore,"slice_header_extension_data_byte");
2461    } 
2462  }
2463#endif
2464  m_pcBitstream->readByteAlignment();
2465
2466  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
2467  {
2468    Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
2469   
2470    // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
2471    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
2472    {
2473      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
2474      {
2475        endOfSliceHeaderLocation++;
2476      }
2477    }
2478
2479    Int  curEntryPointOffset     = 0;
2480    Int  prevEntryPointOffset    = 0;
2481    for (UInt idx=0; idx<numEntryPointOffsets; idx++)
2482    {
2483      curEntryPointOffset += entryPointOffset[ idx ];
2484
2485      Int emulationPreventionByteCount = 0;
2486      for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
2487      {
2488        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) && 
2489             m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
2490        {
2491          emulationPreventionByteCount++;
2492        }
2493      }
2494
2495      entryPointOffset[ idx ] -= emulationPreventionByteCount;
2496      prevEntryPointOffset = curEntryPointOffset;
2497    }
2498
2499    if ( pps->getTilesEnabledFlag() )
2500    {
2501      rpcSlice->setTileLocationCount( numEntryPointOffsets );
2502
2503      UInt prevPos = 0;
2504      for (Int idx=0; idx<rpcSlice->getTileLocationCount(); idx++)
2505      {
2506        rpcSlice->setTileLocation( idx, prevPos + entryPointOffset [ idx ] );
2507        prevPos += entryPointOffset[ idx ];
2508      }
2509    }
2510    else if ( pps->getEntropyCodingSyncEnabledFlag() )
2511    {
2512    Int numSubstreams = rpcSlice->getNumEntryPointOffsets()+1;
2513      rpcSlice->allocSubstreamSizes(numSubstreams);
2514      UInt *pSubstreamSizes       = rpcSlice->getSubstreamSizes();
2515      for (Int idx=0; idx<numSubstreams-1; idx++)
2516      {
2517        if ( idx < numEntryPointOffsets )
2518        {
2519          pSubstreamSizes[ idx ] = ( entryPointOffset[ idx ] << 3 ) ;
2520        }
2521        else
2522        {
2523          pSubstreamSizes[ idx ] = 0;
2524        }
2525      }
2526    }
2527
2528    if (entryPointOffset)
2529    {
2530      delete [] entryPointOffset;
2531    }
2532  }
2533
2534  return;
2535}
2536 
2537Void TDecCavlc::parsePTL( TComPTL *rpcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1 )
2538{
2539  UInt uiCode;
2540  if(profilePresentFlag)
2541  {
2542    parseProfileTier(rpcPTL->getGeneralPTL());
2543  }
2544  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(uiCode);
2545
2546  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
2547  {
2548#if !H_MV
2549    if(profilePresentFlag)
2550    {
2551#endif
2552      READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
2553#if H_MV
2554      // When profilePresentFlag is equal to 0, sub_layer_profile_present_flag[ i ] shall be equal to 0.
2555      assert( profilePresentFlag || !rpcPTL->getSubLayerProfilePresentFlag(i) );
2556#else
2557    }
2558#endif
2559    READ_FLAG( uiCode, "sub_layer_level_present_flag[i]"   ); rpcPTL->setSubLayerLevelPresentFlag  (i, uiCode);
2560  }
2561 
2562  if (maxNumSubLayersMinus1 > 0)
2563  {
2564    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
2565    {
2566      READ_CODE(2, uiCode, "reserved_zero_2bits");
2567      assert(uiCode == 0);
2568    }
2569  }
2570 
2571  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
2572  {
2573#if H_MV
2574    if( rpcPTL->getSubLayerProfilePresentFlag(i) )         
2575#else
2576    if( profilePresentFlag && rpcPTL->getSubLayerProfilePresentFlag(i) )         
2577#endif
2578    {
2579      parseProfileTier(rpcPTL->getSubLayerPTL(i));
2580    }
2581    if(rpcPTL->getSubLayerLevelPresentFlag(i))
2582    {
2583      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(uiCode);
2584    }
2585  }
2586}
2587
2588Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl)
2589{
2590  UInt uiCode;
2591  READ_CODE(2 , uiCode, "XXX_profile_space[]");   ptl->setProfileSpace(uiCode);
2592  READ_FLAG(    uiCode, "XXX_tier_flag[]"    );   ptl->setTierFlag    (uiCode ? 1 : 0);
2593  READ_CODE(5 , uiCode, "XXX_profile_idc[]"  );   ptl->setProfileIdc  (uiCode);
2594  for(Int j = 0; j < 32; j++)
2595  {
2596    READ_FLAG(  uiCode, "XXX_profile_compatibility_flag[][j]");   ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
2597  }
2598  READ_FLAG(uiCode, "general_progressive_source_flag");
2599  ptl->setProgressiveSourceFlag(uiCode ? true : false);
2600
2601  READ_FLAG(uiCode, "general_interlaced_source_flag");
2602  ptl->setInterlacedSourceFlag(uiCode ? true : false);
2603 
2604  READ_FLAG(uiCode, "general_non_packed_constraint_flag");
2605  ptl->setNonPackedConstraintFlag(uiCode ? true : false);
2606 
2607  READ_FLAG(uiCode, "general_frame_only_constraint_flag");
2608  ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
2609 
2610#if H_MV
2611  if( ptl->getV2ConstraintsPresentFlag() )
2612  {
2613    READ_FLAG( uiCode, "max_12bit_constraint_flag" );        ptl->setMax12bitConstraintFlag      ( uiCode == 1 );
2614    READ_FLAG( uiCode, "max_10bit_constraint_flag" );        ptl->setMax10bitConstraintFlag      ( uiCode == 1 );
2615    READ_FLAG( uiCode, "max_8bit_constraint_flag" );         ptl->setMax8bitConstraintFlag       ( uiCode == 1 );
2616    READ_FLAG( uiCode, "max_422chroma_constraint_flag" );    ptl->setMax422chromaConstraintFlag  ( uiCode == 1 );
2617    READ_FLAG( uiCode, "max_420chroma_constraint_flag" );    ptl->setMax420chromaConstraintFlag  ( uiCode == 1 );
2618    READ_FLAG( uiCode, "max_monochrome_constraint_flag" );   ptl->setMaxMonochromeConstraintFlag ( uiCode == 1 );
2619    READ_FLAG( uiCode, "intra_constraint_flag" );            ptl->setIntraConstraintFlag         ( uiCode == 1 );
2620    READ_FLAG( uiCode, "one_picture_only_constraint_flag" ); ptl->setOnePictureOnlyConstraintFlag( uiCode == 1 );
2621    READ_FLAG( uiCode, "lower_bit_rate_constraint_flag" );   ptl->setLowerBitRateConstraintFlag  ( uiCode == 1 );   
2622    READ_CODE(16, uiCode, "XXX_reserved_zero_34bits[0..15]");
2623    READ_CODE(16, uiCode, "XXX_reserved_zero_34bits[16..31]");
2624    READ_CODE(2 , uiCode, "XXX_reserved_zero_34bits[32..33]");
2625  }
2626  else
2627  {
2628    READ_CODE(16, uiCode, "XXX_reserved_zero_43bits[0..15]");
2629    READ_CODE(16, uiCode, "XXX_reserved_zero_43bits[16..31]");
2630    READ_CODE(11, uiCode, "XXX_reserved_zero_43bits[32..42]");
2631  }
2632  if( ptl->getInbldPresentFlag() )
2633  {
2634    READ_FLAG( uiCode, "inbld_flag" ); ptl->setInbldFlag( uiCode == 1 );
2635  }
2636  else
2637  {
2638    READ_FLAG(uiCode, "reserved_zero_bit");
2639  }
2640#else
2641  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[0..15]");
2642  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[16..31]");
2643  READ_CODE(12, uiCode, "XXX_reserved_zero_44bits[32..43]");
2644#endif
2645}
2646
2647Void TDecCavlc::parseTerminatingBit( UInt& ruiBit )
2648{
2649  ruiBit = false;
2650  Int iBitsLeft = m_pcBitstream->getNumBitsLeft();
2651  if(iBitsLeft <= 8)
2652  {
2653    UInt uiPeekValue = m_pcBitstream->peekBits(iBitsLeft);
2654    if (uiPeekValue == (1<<(iBitsLeft-1)))
2655    {
2656      ruiBit = true;
2657    }
2658  }
2659}
2660
2661Void TDecCavlc::parseSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2662{
2663  assert(0);
2664}
2665Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2666{
2667  assert(0);
2668}
2669
2670Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
2671{
2672  assert(0);
2673}
2674
2675Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2676{
2677  assert(0);
2678}
2679
2680Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2681{
2682  assert(0);
2683}
2684
2685Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2686{
2687  assert(0);
2688}
2689
2690/** Parse I_PCM information.
2691* \param pcCU pointer to CU
2692* \param uiAbsPartIdx CU index
2693* \param uiDepth CU depth
2694* \returns Void
2695*
2696* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes. 
2697*/
2698Void TDecCavlc::parseIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2699{
2700  assert(0);
2701}
2702
2703Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2704{ 
2705  assert(0);
2706}
2707
2708Void TDecCavlc::parseIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2709{
2710  assert(0);
2711}
2712
2713Void TDecCavlc::parseInterDir( TComDataCU* /*pcCU*/, UInt& /*ruiInterDir*/, UInt /*uiAbsPartIdx*/ )
2714{
2715  assert(0);
2716}
2717
2718Void TDecCavlc::parseRefFrmIdx( TComDataCU* /*pcCU*/, Int& /*riRefFrmIdx*/, RefPicList /*eRefList*/ )
2719{
2720  assert(0);
2721}
2722
2723Void TDecCavlc::parseMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiPartIdx*/, UInt /*uiDepth*/, RefPicList /*eRefList*/ )
2724{
2725  assert(0);
2726}
2727
2728Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2729{
2730  Int qp;
2731  Int  iDQp;
2732
2733  xReadSvlc( iDQp );
2734
2735  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffsetY();
2736  qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
2737
2738  UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1) ;
2739  UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
2740
2741  pcCU->setQPSubParts( qp, uiAbsQpCUPartIdx, uiQpCUDepth );
2742}
2743
2744Void TDecCavlc::parseCoeffNxN( TComDataCU* /*pcCU*/, TCoeff* /*pcCoef*/, UInt /*uiAbsPartIdx*/, UInt /*uiWidth*/, UInt /*uiHeight*/, UInt /*uiDepth*/, TextType /*eTType*/ )
2745{
2746  assert(0);
2747}
2748
2749Void TDecCavlc::parseTransformSubdivFlag( UInt& /*ruiSubdivFlag*/, UInt /*uiLog2TransformBlockSize*/ )
2750{
2751  assert(0);
2752}
2753
2754Void TDecCavlc::parseQtCbf( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, TextType /*eType*/, UInt /*uiTrDepth*/, UInt /*uiDepth*/ )
2755{
2756  assert(0);
2757}
2758
2759Void TDecCavlc::parseQtRootCbf( UInt /*uiAbsPartIdx*/, UInt& /*uiQtRootCbf*/ )
2760{
2761  assert(0);
2762}
2763
2764Void TDecCavlc::parseTransformSkipFlags (TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*width*/, UInt /*height*/, UInt /*uiDepth*/, TextType /*eTType*/)
2765{
2766  assert(0);
2767}
2768
2769Void TDecCavlc::parseMergeFlag ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiPUIdx*/ )
2770{
2771  assert(0);
2772}
2773
2774Void TDecCavlc::parseMergeIndex ( TComDataCU* /*pcCU*/, UInt& /*ruiMergeIndex*/ )
2775{
2776  assert(0);
2777}
2778
2779// ====================================================================================================================
2780// Protected member functions
2781// ====================================================================================================================
2782
2783/** parse explicit wp tables
2784* \param TComSlice* pcSlice
2785* \returns Void
2786*/
2787Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice )
2788{
2789  wpScalingParam  *wp;
2790  Bool            bChroma     = true; // color always present in HEVC ?
2791  SliceType       eSliceType  = pcSlice->getSliceType();
2792  Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
2793  UInt            uiLog2WeightDenomLuma, uiLog2WeightDenomChroma;
2794  UInt            uiTotalSignalledWeightFlags = 0;
2795 
2796  Int iDeltaDenom;
2797  // decode delta_luma_log2_weight_denom :
2798  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );     // ue(v): luma_log2_weight_denom
2799  assert( uiLog2WeightDenomLuma <= 7 );
2800  if( bChroma ) 
2801  {
2802    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );     // se(v): delta_chroma_log2_weight_denom
2803    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)>=0);
2804    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)<=7);
2805    uiLog2WeightDenomChroma = (UInt)(iDeltaDenom + uiLog2WeightDenomLuma);
2806  }
2807
2808  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) 
2809  {
2810    RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2811    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
2812    {
2813      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2814
2815      wp[0].uiLog2WeightDenom = uiLog2WeightDenomLuma;
2816      wp[1].uiLog2WeightDenom = uiLog2WeightDenomChroma;
2817      wp[2].uiLog2WeightDenom = uiLog2WeightDenomChroma;
2818
2819      UInt  uiCode;
2820      READ_FLAG( uiCode, "luma_weight_lX_flag" );           // u(1): luma_weight_l0_flag
2821      wp[0].bPresentFlag = ( uiCode == 1 );
2822      uiTotalSignalledWeightFlags += wp[0].bPresentFlag;
2823    }
2824    if ( bChroma ) 
2825    {
2826      UInt  uiCode;
2827      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
2828      {
2829        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2830        READ_FLAG( uiCode, "chroma_weight_lX_flag" );      // u(1): chroma_weight_l0_flag
2831        wp[1].bPresentFlag = ( uiCode == 1 );
2832        wp[2].bPresentFlag = ( uiCode == 1 );
2833        uiTotalSignalledWeightFlags += 2*wp[1].bPresentFlag;
2834      }
2835    }
2836    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
2837    {
2838      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2839      if ( wp[0].bPresentFlag ) 
2840      {
2841        Int iDeltaWeight;
2842        READ_SVLC( iDeltaWeight, "delta_luma_weight_lX" );  // se(v): delta_luma_weight_l0[i]
2843        assert( iDeltaWeight >= -128 );
2844        assert( iDeltaWeight <=  127 );
2845        wp[0].iWeight = (iDeltaWeight + (1<<wp[0].uiLog2WeightDenom));
2846        READ_SVLC( wp[0].iOffset, "luma_offset_lX" );       // se(v): luma_offset_l0[i]
2847        assert( wp[0].iOffset >= -128 );
2848        assert( wp[0].iOffset <=  127 );
2849      }
2850      else 
2851      {
2852        wp[0].iWeight = (1 << wp[0].uiLog2WeightDenom);
2853        wp[0].iOffset = 0;
2854      }
2855      if ( bChroma ) 
2856      {
2857        if ( wp[1].bPresentFlag ) 
2858        {
2859          for ( Int j=1 ; j<3 ; j++ ) 
2860          {
2861            Int iDeltaWeight;
2862            READ_SVLC( iDeltaWeight, "delta_chroma_weight_lX" );  // se(v): chroma_weight_l0[i][j]
2863            assert( iDeltaWeight >= -128 );
2864            assert( iDeltaWeight <=  127 );
2865            wp[j].iWeight = (iDeltaWeight + (1<<wp[1].uiLog2WeightDenom));
2866
2867            Int iDeltaChroma;
2868            READ_SVLC( iDeltaChroma, "delta_chroma_offset_lX" );  // se(v): delta_chroma_offset_l0[i][j]
2869            assert( iDeltaChroma >= -512 );
2870            assert( iDeltaChroma <=  511 );
2871            Int pred = ( 128 - ( ( 128*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
2872            wp[j].iOffset = Clip3(-128, 127, (iDeltaChroma + pred) );
2873          }
2874        }
2875        else 
2876        {
2877          for ( Int j=1 ; j<3 ; j++ ) 
2878          {
2879            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
2880            wp[j].iOffset = 0;
2881          }
2882        }
2883      }
2884    }
2885
2886    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ ) 
2887    {
2888      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2889
2890      wp[0].bPresentFlag = false;
2891      wp[1].bPresentFlag = false;
2892      wp[2].bPresentFlag = false;
2893    }
2894  }
2895  assert(uiTotalSignalledWeightFlags<=24);
2896}
2897
2898/** decode quantization matrix
2899* \param scalingList quantization matrix information
2900*/
2901Void TDecCavlc::parseScalingList(TComScalingList* scalingList)
2902{
2903  UInt  code, sizeId, listId;
2904  Bool scalingListPredModeFlag;
2905  //for each size
2906  for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2907  {
2908    for(listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
2909    {
2910      READ_FLAG( code, "scaling_list_pred_mode_flag");
2911      scalingListPredModeFlag = (code) ? true : false;
2912      if(!scalingListPredModeFlag) //Copy Mode
2913      {
2914        READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
2915        scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
2916        if( sizeId > SCALING_LIST_8x8 )
2917        {
2918          scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
2919        }
2920        scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
2921
2922      }
2923      else //DPCM Mode
2924      {
2925        xDecodeScalingList(scalingList, sizeId, listId);
2926      }
2927    }
2928  }
2929
2930  return;
2931}
2932/** decode DPCM
2933* \param scalingList  quantization matrix information
2934* \param sizeId size index
2935* \param listId list index
2936*/
2937Void TDecCavlc::xDecodeScalingList(TComScalingList *scalingList, UInt sizeId, UInt listId)
2938{
2939  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
2940  Int data;
2941  Int scalingListDcCoefMinus8 = 0;
2942  Int nextCoef = SCALING_LIST_START_VALUE;
2943  UInt* scan  = (sizeId == 0) ? g_auiSigLastScan [ SCAN_DIAG ] [ 1 ] :  g_sigLastScanCG32x32;
2944  Int *dst = scalingList->getScalingListAddress(sizeId, listId);
2945
2946  if( sizeId > SCALING_LIST_8x8 )
2947  {
2948    READ_SVLC( scalingListDcCoefMinus8, "scaling_list_dc_coef_minus8");
2949    scalingList->setScalingListDC(sizeId,listId,scalingListDcCoefMinus8 + 8);
2950    nextCoef = scalingList->getScalingListDC(sizeId,listId);
2951  }
2952
2953  for(i = 0; i < coefNum; i++)
2954  {
2955    READ_SVLC( data, "scaling_list_delta_coef");
2956    nextCoef = (nextCoef + data + 256 ) % 256;
2957    dst[scan[i]] = nextCoef;
2958  }
2959}
2960
2961Bool TDecCavlc::xMoreRbspData()
2962{ 
2963  Int bitsLeft = m_pcBitstream->getNumBitsLeft();
2964
2965  // if there are more than 8 bits, it cannot be rbsp_trailing_bits
2966  if (bitsLeft > 8)
2967  {
2968    return true;
2969  }
2970
2971  UChar lastByte = m_pcBitstream->peekBits(bitsLeft);
2972  Int cnt = bitsLeft;
2973
2974  // remove trailing bits equal to zero
2975  while ((cnt>0) && ((lastByte & 1) == 0))
2976  {
2977    lastByte >>= 1;
2978    cnt--;
2979  }
2980  // remove bit equal to one
2981  cnt--;
2982
2983  // we should not have a negative number of bits
2984  assert (cnt>=0);
2985
2986  // we have more data, if cnt is not zero
2987  return (cnt>0);
2988}
2989
2990//! \}
2991
Note: See TracBrowser for help on using the repository browser.