source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp @ 1204

Last change on this file since 1204 was 1203, checked in by seregin, 10 years ago

macro cleanup: REPN_FORMAT_IN_VPS

  • Property svn:eol-style set to native
File size: 103.6 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     TEncCavlc.cpp
35    \brief    CAVLC encoder class
36*/
37
38#include "../TLibCommon/CommonDef.h"
39#include "TEncCavlc.h"
40#include "SEIwrite.h"
41
42//! \ingroup TLibEncoder
43//! \{
44
45#if ENC_DEC_TRACE
46
47Void  xTraceSPSHeader (TComSPS *pSPS)
48{
49  fprintf( g_hTrace, "=========== Sequence Parameter Set ID: %d ===========\n", pSPS->getSPSId() );
50}
51
52Void  xTracePPSHeader (TComPPS *pPPS)
53{
54  fprintf( g_hTrace, "=========== Picture Parameter Set ID: %d ===========\n", pPPS->getPPSId() );
55}
56
57Void  xTraceSliceHeader (TComSlice *pSlice)
58{
59  fprintf( g_hTrace, "=========== Slice ===========\n");
60}
61
62#endif
63
64
65
66// ====================================================================================================================
67// Constructor / destructor / create / destroy
68// ====================================================================================================================
69
70TEncCavlc::TEncCavlc()
71{
72  m_pcBitIf           = NULL;
73}
74
75TEncCavlc::~TEncCavlc()
76{
77}
78
79
80// ====================================================================================================================
81// Public member functions
82// ====================================================================================================================
83
84Void TEncCavlc::resetEntropy()
85{
86}
87
88
89Void TEncCavlc::codeDFFlag(UInt uiCode, const Char *pSymbolName)
90{
91  WRITE_FLAG(uiCode, pSymbolName);
92}
93Void TEncCavlc::codeDFSvlc(Int iCode, const Char *pSymbolName)
94{
95  WRITE_SVLC(iCode, pSymbolName);
96}
97
98Void TEncCavlc::codeShortTermRefPicSet( TComSPS* pcSPS, TComReferencePictureSet* rps, Bool calledFromSliceHeader, Int idx)
99{
100#if PRINT_RPS_INFO
101  Int lastBits = getNumberOfWrittenBits();
102#endif
103  if (idx > 0)
104  {
105  WRITE_FLAG( rps->getInterRPSPrediction(), "inter_ref_pic_set_prediction_flag" ); // inter_RPS_prediction_flag
106  }
107  if (rps->getInterRPSPrediction())
108  {
109    Int deltaRPS = rps->getDeltaRPS();
110    if(calledFromSliceHeader)
111    {
112      WRITE_UVLC( rps->getDeltaRIdxMinus1(), "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
113    }
114
115    WRITE_CODE( (deltaRPS >=0 ? 0: 1), 1, "delta_rps_sign" ); //delta_rps_sign
116    WRITE_UVLC( abs(deltaRPS) - 1, "abs_delta_rps_minus1"); // absolute delta RPS minus 1
117
118    for(Int j=0; j < rps->getNumRefIdc(); j++)
119    {
120      Int refIdc = rps->getRefIdc(j);
121      WRITE_CODE( (refIdc==1? 1: 0), 1, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
122      if (refIdc != 1)
123      {
124        WRITE_CODE( refIdc>>1, 1, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
125      }
126    }
127  }
128  else
129  {
130    WRITE_UVLC( rps->getNumberOfNegativePictures(), "num_negative_pics" );
131    WRITE_UVLC( rps->getNumberOfPositivePictures(), "num_positive_pics" );
132    Int prev = 0;
133    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
134    {
135      WRITE_UVLC( prev-rps->getDeltaPOC(j)-1, "delta_poc_s0_minus1" );
136      prev = rps->getDeltaPOC(j);
137      WRITE_FLAG( rps->getUsed(j), "used_by_curr_pic_s0_flag");
138    }
139    prev = 0;
140    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
141    {
142      WRITE_UVLC( rps->getDeltaPOC(j)-prev-1, "delta_poc_s1_minus1" );
143      prev = rps->getDeltaPOC(j);
144      WRITE_FLAG( rps->getUsed(j), "used_by_curr_pic_s1_flag" );
145    }
146  }
147
148#if PRINT_RPS_INFO
149  printf("irps=%d (%2d bits) ", rps->getInterRPSPrediction(), getNumberOfWrittenBits() - lastBits);
150  rps->printDeltaPOC();
151#endif
152}
153
154
155Void TEncCavlc::codePPS( TComPPS* pcPPS
156#if Q0048_CGS_3D_ASYMLUT
157  , TEnc3DAsymLUT * pc3DAsymLUT
158#endif
159  )
160{
161#if ENC_DEC_TRACE
162  xTracePPSHeader (pcPPS);
163#endif
164
165  const UInt numberValidComponents = getNumberValidComponents(pcPPS->getSPS()->getChromaFormatIdc());
166
167  WRITE_UVLC( pcPPS->getPPSId(),                             "pps_pic_parameter_set_id" );
168  WRITE_UVLC( pcPPS->getSPSId(),                             "pps_seq_parameter_set_id" );
169  WRITE_FLAG( pcPPS->getDependentSliceSegmentsEnabledFlag()    ? 1 : 0, "dependent_slice_segments_enabled_flag" );
170  WRITE_FLAG( pcPPS->getOutputFlagPresentFlag() ? 1 : 0,     "output_flag_present_flag" );
171  WRITE_CODE( pcPPS->getNumExtraSliceHeaderBits(), 3,        "num_extra_slice_header_bits");
172  WRITE_FLAG( pcPPS->getSignHideFlag(), "sign_data_hiding_flag" );
173  WRITE_FLAG( pcPPS->getCabacInitPresentFlag() ? 1 : 0,   "cabac_init_present_flag" );
174  WRITE_UVLC( pcPPS->getNumRefIdxL0DefaultActive()-1,     "num_ref_idx_l0_default_active_minus1");
175  WRITE_UVLC( pcPPS->getNumRefIdxL1DefaultActive()-1,     "num_ref_idx_l1_default_active_minus1");
176
177  WRITE_SVLC( pcPPS->getPicInitQPMinus26(),                  "init_qp_minus26");
178  WRITE_FLAG( pcPPS->getConstrainedIntraPred() ? 1 : 0,      "constrained_intra_pred_flag" );
179  WRITE_FLAG( pcPPS->getUseTransformSkip() ? 1 : 0,  "transform_skip_enabled_flag" );
180  WRITE_FLAG( pcPPS->getUseDQP() ? 1 : 0, "cu_qp_delta_enabled_flag" );
181  if ( pcPPS->getUseDQP() )
182  {
183    WRITE_UVLC( pcPPS->getMaxCuDQPDepth(), "diff_cu_qp_delta_depth" );
184  }
185
186  WRITE_SVLC( COMPONENT_Cb<numberValidComponents ?  (pcPPS->getQpOffset(COMPONENT_Cb)) : 0, "pps_cb_qp_offset" );
187  WRITE_SVLC( COMPONENT_Cr<numberValidComponents ?  (pcPPS->getQpOffset(COMPONENT_Cr)) : 0, "pps_cr_qp_offset" );
188
189  assert(numberValidComponents <= 3); // if more than 3 components (eg 4:4:4:4), then additional offsets will have to go in extension area...
190
191  WRITE_FLAG( pcPPS->getSliceChromaQpFlag() ? 1 : 0,          "pps_slice_chroma_qp_offsets_present_flag" );
192
193  WRITE_FLAG( pcPPS->getUseWP() ? 1 : 0,  "weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
194  WRITE_FLAG( pcPPS->getWPBiPred() ? 1 : 0, "weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
195  WRITE_FLAG( pcPPS->getTransquantBypassEnableFlag() ? 1 : 0, "transquant_bypass_enable_flag" );
196  WRITE_FLAG( pcPPS->getTilesEnabledFlag()             ? 1 : 0, "tiles_enabled_flag" );
197  WRITE_FLAG( pcPPS->getEntropyCodingSyncEnabledFlag() ? 1 : 0, "entropy_coding_sync_enabled_flag" );
198  if( pcPPS->getTilesEnabledFlag() )
199  {
200    WRITE_UVLC( pcPPS->getNumTileColumnsMinus1(),                                    "num_tile_columns_minus1" );
201    WRITE_UVLC( pcPPS->getNumTileRowsMinus1(),                                       "num_tile_rows_minus1" );
202    WRITE_FLAG( pcPPS->getTileUniformSpacingFlag(),                                  "uniform_spacing_flag" );
203    if( !pcPPS->getTileUniformSpacingFlag() )
204    {
205      for(UInt i=0; i<pcPPS->getNumTileColumnsMinus1(); i++)
206      {
207        WRITE_UVLC( pcPPS->getTileColumnWidth(i)-1,                                  "column_width_minus1" );
208      }
209      for(UInt i=0; i<pcPPS->getNumTileRowsMinus1(); i++)
210      {
211        WRITE_UVLC( pcPPS->getTileRowHeight(i)-1,                                    "row_height_minus1" );
212      }
213    }
214    if(pcPPS->getNumTileColumnsMinus1() !=0 || pcPPS->getNumTileRowsMinus1() !=0)
215    {
216      WRITE_FLAG( pcPPS->getLoopFilterAcrossTilesEnabledFlag()?1 : 0,          "loop_filter_across_tiles_enabled_flag");
217    }
218  }
219  WRITE_FLAG( pcPPS->getLoopFilterAcrossSlicesEnabledFlag()?1 : 0,        "loop_filter_across_slices_enabled_flag");
220  WRITE_FLAG( pcPPS->getDeblockingFilterControlPresentFlag()?1 : 0,       "deblocking_filter_control_present_flag");
221  if(pcPPS->getDeblockingFilterControlPresentFlag())
222  {
223    WRITE_FLAG( pcPPS->getDeblockingFilterOverrideEnabledFlag() ? 1 : 0,  "deblocking_filter_override_enabled_flag" );
224    WRITE_FLAG( pcPPS->getPicDisableDeblockingFilterFlag() ? 1 : 0,       "pps_disable_deblocking_filter_flag" );
225    if(!pcPPS->getPicDisableDeblockingFilterFlag())
226    {
227      WRITE_SVLC( pcPPS->getDeblockingFilterBetaOffsetDiv2(),             "pps_beta_offset_div2" );
228      WRITE_SVLC( pcPPS->getDeblockingFilterTcOffsetDiv2(),               "pps_tc_offset_div2" );
229    }
230  }
231  WRITE_FLAG( pcPPS->getScalingListPresentFlag() ? 1 : 0,                          "pps_scaling_list_data_present_flag" );
232  if( pcPPS->getScalingListPresentFlag() )
233  {
234    codeScalingList( m_pcSlice->getScalingList() );
235  }
236  WRITE_FLAG( pcPPS->getListsModificationPresentFlag(), "lists_modification_present_flag");
237  WRITE_UVLC( pcPPS->getLog2ParallelMergeLevelMinus2(), "log2_parallel_merge_level_minus2");
238  WRITE_FLAG( pcPPS->getSliceHeaderExtensionPresentFlag() ? 1 : 0, "slice_segment_header_extension_present_flag");
239
240  Bool pps_extension_present_flag=false;
241  Bool pps_extension_flags[NUM_PPS_EXTENSION_FLAGS]={false};
242
243  pps_extension_flags[PPS_EXT__REXT] = (
244             ( pcPPS->getUseTransformSkip() && (pcPPS->getTransformSkipLog2MaxSize() != 2))
245          || pcPPS->getUseCrossComponentPrediction()
246          || ( pcPPS->getChromaQpAdjTableSize() > 0 )
247          || ( pcPPS->getSaoOffsetBitShift(CHANNEL_TYPE_LUMA) !=0 ) || ( pcPPS->getSaoOffsetBitShift(CHANNEL_TYPE_CHROMA) !=0 )
248     )
249    ;
250
251  // Other PPS extension flags checked here.
252
253#if SVC_EXTENSION
254  pps_extension_flags[PPS_EXT__MLAYER] = pcPPS->getExtensionFlag() ? 1 : 0;
255#if Q0048_CGS_3D_ASYMLUT
256  UInt bits = 0;
257#endif
258#endif
259
260  for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
261  {
262    pps_extension_present_flag|=pps_extension_flags[i];
263  }
264
265  WRITE_FLAG( (pps_extension_present_flag?1:0), "pps_extension_present_flag" );
266
267  if (pps_extension_present_flag)
268  {
269    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
270    {
271      WRITE_FLAG( pps_extension_flags[i]?1:0, "pps_extension_flag[]" );
272    }
273
274    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
275    {
276      if (pps_extension_flags[i])
277      {
278        switch (PPSExtensionFlagIndex(i))
279        {
280          case PPS_EXT__REXT:
281
282            if (pcPPS->getUseTransformSkip())
283            {
284              WRITE_UVLC( pcPPS->getTransformSkipLog2MaxSize()-2,                 "log2_transform_skip_max_size_minus2");
285            }
286
287            WRITE_FLAG((pcPPS->getUseCrossComponentPrediction() ? 1 : 0),         "cross_component_prediction_flag" );
288
289            WRITE_FLAG(UInt(pcPPS->getChromaQpAdjTableSize() > 0),                "chroma_qp_adjustment_enabled_flag" );
290            if (pcPPS->getChromaQpAdjTableSize() > 0)
291            {
292              WRITE_UVLC(pcPPS->getMaxCuChromaQpAdjDepth(),                       "diff_cu_chroma_qp_adjustment_depth");
293              WRITE_UVLC(pcPPS->getChromaQpAdjTableSize() - 1,                    "chroma_qp_adjustment_table_size_minus1");
294              /* skip zero index */
295              for (Int chromaQpAdjustmentIndex = 1; chromaQpAdjustmentIndex <= pcPPS->getChromaQpAdjTableSize(); chromaQpAdjustmentIndex++)
296              {
297                WRITE_SVLC(pcPPS->getChromaQpAdjTableAt(chromaQpAdjustmentIndex).u.comp.CbOffset,     "cb_qp_adjustnemt[i]");
298                WRITE_SVLC(pcPPS->getChromaQpAdjTableAt(chromaQpAdjustmentIndex).u.comp.CrOffset,     "cr_qp_adjustnemt[i]");
299              }
300            }
301
302            WRITE_UVLC( pcPPS->getSaoOffsetBitShift(CHANNEL_TYPE_LUMA),           "sao_luma_bit_shift"   );
303            WRITE_UVLC( pcPPS->getSaoOffsetBitShift(CHANNEL_TYPE_CHROMA),         "sao_chroma_bit_shift" );
304            break;
305#if SVC_EXTENSION
306          case PPS_EXT__MLAYER:
307            WRITE_FLAG( pcPPS->getPocResetInfoPresentFlag() ? 1 : 0, "poc_reset_info_present_flag" );
308#if SCALINGLIST_INFERRING
309            WRITE_FLAG( pcPPS->getInferScalingListFlag() ? 1 : 0, "pps_infer_scaling_list_flag" );
310            if( pcPPS->getInferScalingListFlag() )
311            {
312              // The value of pps_scaling_list_ref_layer_id shall be in the range of 0 to 62, inclusive
313              assert( pcPPS->getScalingListRefLayerId() <= 62 );
314              WRITE_CODE( pcPPS->getScalingListRefLayerId(), 6, "pps_scaling_list_ref_layer_id" );
315            }
316#endif
317
318            WRITE_UVLC( pcPPS->getNumRefLayerLocationOffsets(),      "num_ref_loc_offsets" );
319            for(Int k = 0; k < pcPPS->getNumRefLayerLocationOffsets(); k++)
320            {
321              WRITE_CODE( pcPPS->getRefLocationOffsetLayerId(k), 6, "ref_loc_offset_layer_id" );
322              WRITE_FLAG( pcPPS->getScaledRefLayerOffsetPresentFlag(k) ? 1 : 0, "scaled_ref_layer_offset_prsent_flag" );
323              if (pcPPS->getScaledRefLayerOffsetPresentFlag(k))
324              {
325                Window scaledWindow = pcPPS->getScaledRefLayerWindow(k);
326                WRITE_SVLC( scaledWindow.getWindowLeftOffset()   >> 1, "scaled_ref_layer_left_offset" );
327                WRITE_SVLC( scaledWindow.getWindowTopOffset()    >> 1, "scaled_ref_layer_top_offset" );
328                WRITE_SVLC( scaledWindow.getWindowRightOffset()  >> 1, "scaled_ref_layer_right_offset" );
329                WRITE_SVLC( scaledWindow.getWindowBottomOffset() >> 1, "scaled_ref_layer_bottom_offset" );
330              }
331              WRITE_FLAG( pcPPS->getRefRegionOffsetPresentFlag(k) ? 1 : 0, "ref_region_offset_prsent_flag" );
332              if (pcPPS->getRefRegionOffsetPresentFlag(k))
333              {
334                Window refWindow = pcPPS->getRefLayerWindow(k);
335                WRITE_SVLC( refWindow.getWindowLeftOffset()   >> 1, "ref_region_left_offset" );
336                WRITE_SVLC( refWindow.getWindowTopOffset()    >> 1, "ref_region_top_offset" );
337                WRITE_SVLC( refWindow.getWindowRightOffset()  >> 1, "ref_region_right_offset" );
338                WRITE_SVLC( refWindow.getWindowBottomOffset() >> 1, "ref_region_bottom_offset" );
339              }
340              WRITE_FLAG( pcPPS->getResamplePhaseSetPresentFlag(k) ? 1 : 0, "resample_phase_set_present_flag" );
341              if (pcPPS->getResamplePhaseSetPresentFlag(k))
342              {
343                WRITE_UVLC( pcPPS->getPhaseHorLuma(k), "phase_hor_luma" );
344                WRITE_UVLC( pcPPS->getPhaseVerLuma(k), "phase_ver_luma" );
345                WRITE_UVLC( pcPPS->getPhaseHorChroma(k) + 8, "phase_hor_chroma_plus8" );
346                WRITE_UVLC( pcPPS->getPhaseVerChroma(k) + 8, "phase_ver_chroma_plus8" );
347              }
348            }
349#if Q0048_CGS_3D_ASYMLUT
350            bits = getNumberOfWrittenBits();
351            WRITE_FLAG( pcPPS->getCGSFlag() , "colour_mapping_enabled_flag" );
352            if( pcPPS->getCGSFlag() )
353            {
354              assert( pc3DAsymLUT != NULL );
355              xCode3DAsymLUT( pc3DAsymLUT );
356            }
357            pc3DAsymLUT->setPPSBit( getNumberOfWrittenBits() - bits );
358#endif
359            break;
360#endif
361          default:
362            assert(pps_extension_flags[i]==false); // Should never get here with an active PPS extension flag.
363            break;
364        } // switch
365      } // if flag present
366    } // loop over PPS flags
367  } // pps_extension_present_flag is non-zero
368}
369
370Void TEncCavlc::codeVUI( TComVUI *pcVUI, TComSPS* pcSPS )
371{
372#if ENC_DEC_TRACE
373  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
374#endif
375  WRITE_FLAG(pcVUI->getAspectRatioInfoPresentFlag(),            "aspect_ratio_info_present_flag");
376  if (pcVUI->getAspectRatioInfoPresentFlag())
377  {
378    WRITE_CODE(pcVUI->getAspectRatioIdc(), 8,                   "aspect_ratio_idc" );
379    if (pcVUI->getAspectRatioIdc() == 255)
380    {
381      WRITE_CODE(pcVUI->getSarWidth(), 16,                      "sar_width");
382      WRITE_CODE(pcVUI->getSarHeight(), 16,                     "sar_height");
383    }
384  }
385  WRITE_FLAG(pcVUI->getOverscanInfoPresentFlag(),               "overscan_info_present_flag");
386  if (pcVUI->getOverscanInfoPresentFlag())
387  {
388    WRITE_FLAG(pcVUI->getOverscanAppropriateFlag(),             "overscan_appropriate_flag");
389  }
390  WRITE_FLAG(pcVUI->getVideoSignalTypePresentFlag(),            "video_signal_type_present_flag");
391  if (pcVUI->getVideoSignalTypePresentFlag())
392  {
393    WRITE_CODE(pcVUI->getVideoFormat(), 3,                      "video_format");
394    WRITE_FLAG(pcVUI->getVideoFullRangeFlag(),                  "video_full_range_flag");
395    WRITE_FLAG(pcVUI->getColourDescriptionPresentFlag(),        "colour_description_present_flag");
396    if (pcVUI->getColourDescriptionPresentFlag())
397    {
398      WRITE_CODE(pcVUI->getColourPrimaries(), 8,                "colour_primaries");
399      WRITE_CODE(pcVUI->getTransferCharacteristics(), 8,        "transfer_characteristics");
400      WRITE_CODE(pcVUI->getMatrixCoefficients(), 8,             "matrix_coefficients");
401    }
402  }
403
404  WRITE_FLAG(pcVUI->getChromaLocInfoPresentFlag(),              "chroma_loc_info_present_flag");
405  if (pcVUI->getChromaLocInfoPresentFlag())
406  {
407    WRITE_UVLC(pcVUI->getChromaSampleLocTypeTopField(),         "chroma_sample_loc_type_top_field");
408    WRITE_UVLC(pcVUI->getChromaSampleLocTypeBottomField(),      "chroma_sample_loc_type_bottom_field");
409  }
410
411  WRITE_FLAG(pcVUI->getNeutralChromaIndicationFlag(),           "neutral_chroma_indication_flag");
412  WRITE_FLAG(pcVUI->getFieldSeqFlag(),                          "field_seq_flag");
413  WRITE_FLAG(pcVUI->getFrameFieldInfoPresentFlag(),             "frame_field_info_present_flag");
414
415  Window defaultDisplayWindow = pcVUI->getDefaultDisplayWindow();
416  WRITE_FLAG(defaultDisplayWindow.getWindowEnabledFlag(),       "default_display_window_flag");
417  if( defaultDisplayWindow.getWindowEnabledFlag() )
418  {
419    WRITE_UVLC(defaultDisplayWindow.getWindowLeftOffset()  / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc()), "def_disp_win_left_offset");
420    WRITE_UVLC(defaultDisplayWindow.getWindowRightOffset() / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc()), "def_disp_win_right_offset");
421    WRITE_UVLC(defaultDisplayWindow.getWindowTopOffset()   / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc()), "def_disp_win_top_offset");
422    WRITE_UVLC(defaultDisplayWindow.getWindowBottomOffset()/ TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc()), "def_disp_win_bottom_offset");
423  }
424  TimingInfo *timingInfo = pcVUI->getTimingInfo();
425  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vui_timing_info_present_flag");
426  if(timingInfo->getTimingInfoPresentFlag())
427  {
428    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vui_num_units_in_tick");
429    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vui_time_scale");
430    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vui_poc_proportional_to_timing_flag");
431    if(timingInfo->getPocProportionalToTimingFlag())
432    {
433      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vui_num_ticks_poc_diff_one_minus1");
434    }
435    WRITE_FLAG(pcVUI->getHrdParametersPresentFlag(),              "hrd_parameters_present_flag");
436    if( pcVUI->getHrdParametersPresentFlag() )
437    {
438      codeHrdParameters(pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
439    }
440  }
441
442  WRITE_FLAG(pcVUI->getBitstreamRestrictionFlag(),              "bitstream_restriction_flag");
443  if (pcVUI->getBitstreamRestrictionFlag())
444  {
445    WRITE_FLAG(pcVUI->getTilesFixedStructureFlag(),             "tiles_fixed_structure_flag");
446    WRITE_FLAG(pcVUI->getMotionVectorsOverPicBoundariesFlag(),  "motion_vectors_over_pic_boundaries_flag");
447    WRITE_FLAG(pcVUI->getRestrictedRefPicListsFlag(),           "restricted_ref_pic_lists_flag");
448    WRITE_UVLC(pcVUI->getMinSpatialSegmentationIdc(),           "min_spatial_segmentation_idc");
449    WRITE_UVLC(pcVUI->getMaxBytesPerPicDenom(),                 "max_bytes_per_pic_denom");
450    WRITE_UVLC(pcVUI->getMaxBitsPerMinCuDenom(),                "max_bits_per_mincu_denom");
451    WRITE_UVLC(pcVUI->getLog2MaxMvLengthHorizontal(),           "log2_max_mv_length_horizontal");
452    WRITE_UVLC(pcVUI->getLog2MaxMvLengthVertical(),             "log2_max_mv_length_vertical");
453  }
454}
455
456Void TEncCavlc::codeHrdParameters( TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1 )
457{
458  if( commonInfPresentFlag )
459  {
460    WRITE_FLAG( hrd->getNalHrdParametersPresentFlag() ? 1 : 0 ,  "nal_hrd_parameters_present_flag" );
461    WRITE_FLAG( hrd->getVclHrdParametersPresentFlag() ? 1 : 0 ,  "vcl_hrd_parameters_present_flag" );
462    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
463    {
464      WRITE_FLAG( hrd->getSubPicCpbParamsPresentFlag() ? 1 : 0,  "sub_pic_cpb_params_present_flag" );
465      if( hrd->getSubPicCpbParamsPresentFlag() )
466      {
467        WRITE_CODE( hrd->getTickDivisorMinus2(), 8,              "tick_divisor_minus2" );
468        WRITE_CODE( hrd->getDuCpbRemovalDelayLengthMinus1(), 5,  "du_cpb_removal_delay_length_minus1" );
469        WRITE_FLAG( hrd->getSubPicCpbParamsInPicTimingSEIFlag() ? 1 : 0, "sub_pic_cpb_params_in_pic_timing_sei_flag" );
470        WRITE_CODE( hrd->getDpbOutputDelayDuLengthMinus1(), 5,   "dpb_output_delay_du_length_minus1"  );
471      }
472      WRITE_CODE( hrd->getBitRateScale(), 4,                     "bit_rate_scale" );
473      WRITE_CODE( hrd->getCpbSizeScale(), 4,                     "cpb_size_scale" );
474      if( hrd->getSubPicCpbParamsPresentFlag() )
475      {
476        WRITE_CODE( hrd->getDuCpbSizeScale(), 4,                "du_cpb_size_scale" );
477      }
478      WRITE_CODE( hrd->getInitialCpbRemovalDelayLengthMinus1(), 5, "initial_cpb_removal_delay_length_minus1" );
479      WRITE_CODE( hrd->getCpbRemovalDelayLengthMinus1(),        5, "au_cpb_removal_delay_length_minus1" );
480      WRITE_CODE( hrd->getDpbOutputDelayLengthMinus1(),         5, "dpb_output_delay_length_minus1" );
481    }
482  }
483  Int i, j, nalOrVcl;
484  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
485  {
486    WRITE_FLAG( hrd->getFixedPicRateFlag( i ) ? 1 : 0,          "fixed_pic_rate_general_flag");
487    if( !hrd->getFixedPicRateFlag( i ) )
488    {
489      WRITE_FLAG( hrd->getFixedPicRateWithinCvsFlag( i ) ? 1 : 0, "fixed_pic_rate_within_cvs_flag");
490    }
491    else
492    {
493      hrd->setFixedPicRateWithinCvsFlag( i, true );
494    }
495    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
496    {
497      WRITE_UVLC( hrd->getPicDurationInTcMinus1( i ),           "elemental_duration_in_tc_minus1");
498    }
499    else
500    {
501      WRITE_FLAG( hrd->getLowDelayHrdFlag( i ) ? 1 : 0,           "low_delay_hrd_flag");
502    }
503    if (!hrd->getLowDelayHrdFlag( i ))
504    {
505      WRITE_UVLC( hrd->getCpbCntMinus1( i ),                      "cpb_cnt_minus1");
506    }
507
508    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
509    {
510      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
511          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
512      {
513        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
514        {
515          WRITE_UVLC( hrd->getBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_value_minus1");
516          WRITE_UVLC( hrd->getCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_value_minus1");
517          if( hrd->getSubPicCpbParamsPresentFlag() )
518          {
519            WRITE_UVLC( hrd->getDuCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_du_value_minus1");
520            WRITE_UVLC( hrd->getDuBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_du_value_minus1");
521          }
522          WRITE_FLAG( hrd->getCbrFlag( i, j, nalOrVcl ) ? 1 : 0, "cbr_flag");
523        }
524      }
525    }
526  }
527}
528
529Void TEncCavlc::codeSPS( TComSPS* pcSPS )
530{
531#if SVC_EXTENSION
532  Bool V1CompatibleSPSFlag = !(pcSPS->getLayerId() != 0 && pcSPS->getNumDirectRefLayers() != 0);
533#endif
534
535  const ChromaFormat format                = pcSPS->getChromaFormatIdc();
536  const Bool         chromaEnabled         = isChromaEnabled(format);
537
538#if ENC_DEC_TRACE
539  xTraceSPSHeader (pcSPS);
540#endif
541  WRITE_CODE( pcSPS->getVPSId (),          4,       "sps_video_parameter_set_id" );
542#if SVC_EXTENSION
543  if(pcSPS->getLayerId() == 0)
544  {
545#endif
546  WRITE_CODE( pcSPS->getMaxTLayers() - 1,  3,       "sps_max_sub_layers_minus1" );
547#if SVC_EXTENSION
548  }
549  else
550  {
551    WRITE_CODE( V1CompatibleSPSFlag ? (pcSPS->getMaxTLayers() - 1) : 7,  3,       "sps_ext_or_max_sub_layers_minus1" );
552  }
553
554  if( V1CompatibleSPSFlag )
555  {
556#endif
557  WRITE_FLAG( pcSPS->getTemporalIdNestingFlag() ? 1 : 0,                             "sps_temporal_id_nesting_flag" );
558  codePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
559#if SVC_EXTENSION
560  }
561#endif
562  WRITE_UVLC( pcSPS->getSPSId (),                   "sps_seq_parameter_set_id" );
563#if SVC_EXTENSION
564  if( !V1CompatibleSPSFlag )
565  {
566    WRITE_FLAG( pcSPS->getUpdateRepFormatFlag(), "update_rep_format_flag" );
567 
568    if( pcSPS->getUpdateRepFormatFlag())
569    {
570      WRITE_CODE( pcSPS->getUpdateRepFormatIndex(), 8,   "sps_rep_format_idx");
571    }
572  }
573  else
574  {
575#endif
576  WRITE_UVLC( Int(pcSPS->getChromaFormatIdc ()),    "chroma_format_idc" );
577  if( format == CHROMA_444 )
578  {
579    WRITE_FLAG( 0,                                  "separate_colour_plane_flag");
580  }
581
582  WRITE_UVLC( pcSPS->getPicWidthInLumaSamples (),   "pic_width_in_luma_samples" );
583  WRITE_UVLC( pcSPS->getPicHeightInLumaSamples(),   "pic_height_in_luma_samples" );
584  Window conf = pcSPS->getConformanceWindow();
585
586  WRITE_FLAG( conf.getWindowEnabledFlag(),          "conformance_window_flag" );
587  if (conf.getWindowEnabledFlag())
588  {
589#if SVC_EXTENSION
590    WRITE_UVLC( conf.getWindowLeftOffset(),   "conf_win_left_offset"   );
591    WRITE_UVLC( conf.getWindowRightOffset(),  "conf_win_right_offset"  );
592    WRITE_UVLC( conf.getWindowTopOffset(),    "conf_win_top_offset"    );
593    WRITE_UVLC( conf.getWindowBottomOffset(), "conf_win_bottom_offset" );
594#else
595    WRITE_UVLC( conf.getWindowLeftOffset()   / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_left_offset" );
596    WRITE_UVLC( conf.getWindowRightOffset()  / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_right_offset" );
597    WRITE_UVLC( conf.getWindowTopOffset()    / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_top_offset" );
598    WRITE_UVLC( conf.getWindowBottomOffset() / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_bottom_offset" );
599#endif
600  }
601#if SVC_EXTENSION
602  }
603
604  if( V1CompatibleSPSFlag )
605  {
606#endif
607  WRITE_UVLC( pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) - 8,                      "bit_depth_luma_minus8" );
608
609  WRITE_UVLC( chromaEnabled ? (pcSPS->getBitDepth(CHANNEL_TYPE_CHROMA) - 8):0,  "bit_depth_chroma_minus8" );
610#if SVC_EXTENSION
611  }
612#endif
613
614  WRITE_UVLC( pcSPS->getBitsForPOC()-4,                 "log2_max_pic_order_cnt_lsb_minus4" );
615
616#if SVC_EXTENSION
617  if( V1CompatibleSPSFlag )
618  {
619#endif
620  const Bool subLayerOrderingInfoPresentFlag = 1;
621  WRITE_FLAG(subLayerOrderingInfoPresentFlag,       "sps_sub_layer_ordering_info_present_flag");
622  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
623  {
624    WRITE_UVLC( pcSPS->getMaxDecPicBuffering(i) - 1,       "sps_max_dec_pic_buffering_minus1[i]" );
625    WRITE_UVLC( pcSPS->getNumReorderPics(i),               "sps_num_reorder_pics[i]" );
626    WRITE_UVLC( pcSPS->getMaxLatencyIncrease(i),           "sps_max_latency_increase_plus1[i]" );
627    if (!subLayerOrderingInfoPresentFlag)
628    {
629      break;
630    }
631  }
632#if SVC_EXTENSION
633  }
634#endif
635  assert( pcSPS->getMaxCUWidth() == pcSPS->getMaxCUHeight() );
636
637  WRITE_UVLC( pcSPS->getLog2MinCodingBlockSize() - 3,                                "log2_min_coding_block_size_minus3" );
638  WRITE_UVLC( pcSPS->getLog2DiffMaxMinCodingBlockSize(),                             "log2_diff_max_min_coding_block_size" );
639  WRITE_UVLC( pcSPS->getQuadtreeTULog2MinSize() - 2,                                 "log2_min_transform_block_size_minus2" );
640  WRITE_UVLC( pcSPS->getQuadtreeTULog2MaxSize() - pcSPS->getQuadtreeTULog2MinSize(), "log2_diff_max_min_transform_block_size" );
641  WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthInter() - 1,                               "max_transform_hierarchy_depth_inter" );
642  WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthIntra() - 1,                               "max_transform_hierarchy_depth_intra" );
643  WRITE_FLAG( pcSPS->getScalingListFlag() ? 1 : 0,                                   "scaling_list_enabled_flag" );
644  if(pcSPS->getScalingListFlag())
645  {
646#if SCALINGLIST_INFERRING
647    if( !V1CompatibleSPSFlag )
648    {
649      WRITE_FLAG( pcSPS->getInferScalingListFlag() ? 1 : 0, "sps_infer_scaling_list_flag" );
650    }
651
652    if( pcSPS->getInferScalingListFlag() )
653    {
654      // The value of pps_scaling_list_ref_layer_id shall be in the range of 0 to 62, inclusive
655      assert( pcSPS->getScalingListRefLayerId() <= 62 );
656
657      WRITE_CODE( pcSPS->getScalingListRefLayerId(), 6, "sps_scaling_list_ref_layer_id" );
658    }
659    else
660    {
661#endif
662    WRITE_FLAG( pcSPS->getScalingListPresentFlag() ? 1 : 0,                          "sps_scaling_list_data_present_flag" );
663    if(pcSPS->getScalingListPresentFlag())
664    {
665      codeScalingList( m_pcSlice->getScalingList() );
666    }
667#if SCALINGLIST_INFERRING
668    }
669#endif
670  }
671  WRITE_FLAG( pcSPS->getUseAMP() ? 1 : 0,                                            "amp_enabled_flag" );
672  WRITE_FLAG( pcSPS->getUseSAO() ? 1 : 0,                                            "sample_adaptive_offset_enabled_flag");
673
674  WRITE_FLAG( pcSPS->getUsePCM() ? 1 : 0,                                            "pcm_enabled_flag");
675  if( pcSPS->getUsePCM() )
676  {
677    WRITE_CODE( pcSPS->getPCMBitDepth(CHANNEL_TYPE_LUMA) - 1, 4,                            "pcm_sample_bit_depth_luma_minus1" );
678    WRITE_CODE( chromaEnabled ? (pcSPS->getPCMBitDepth(CHANNEL_TYPE_CHROMA) - 1) : 0, 4,    "pcm_sample_bit_depth_chroma_minus1" );
679    WRITE_UVLC( pcSPS->getPCMLog2MinSize() - 3,                                      "log2_min_pcm_luma_coding_block_size_minus3" );
680    WRITE_UVLC( pcSPS->getPCMLog2MaxSize() - pcSPS->getPCMLog2MinSize(),             "log2_diff_max_min_pcm_luma_coding_block_size" );
681    WRITE_FLAG( pcSPS->getPCMFilterDisableFlag()?1 : 0,                              "pcm_loop_filter_disable_flag");
682  }
683
684  assert( pcSPS->getMaxTLayers() > 0 );
685
686  TComRPSList* rpsList = pcSPS->getRPSList();
687  TComReferencePictureSet*      rps;
688
689  WRITE_UVLC(rpsList->getNumberOfReferencePictureSets(), "num_short_term_ref_pic_sets" );
690  for(Int i=0; i < rpsList->getNumberOfReferencePictureSets(); i++)
691  {
692    rps = rpsList->getReferencePictureSet(i);
693    codeShortTermRefPicSet(pcSPS,rps,false, i);
694  }
695  WRITE_FLAG( pcSPS->getLongTermRefsPresent() ? 1 : 0,         "long_term_ref_pics_present_flag" );
696  if (pcSPS->getLongTermRefsPresent())
697  {
698    WRITE_UVLC(pcSPS->getNumLongTermRefPicSPS(), "num_long_term_ref_pic_sps" );
699    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
700    {
701      WRITE_CODE( pcSPS->getLtRefPicPocLsbSps(k), pcSPS->getBitsForPOC(), "lt_ref_pic_poc_lsb_sps");
702      WRITE_FLAG( pcSPS->getUsedByCurrPicLtSPSFlag(k), "used_by_curr_pic_lt_sps_flag");
703    }
704  }
705  WRITE_FLAG( pcSPS->getTMVPFlagsPresent()  ? 1 : 0,           "sps_temporal_mvp_enable_flag" );
706
707  WRITE_FLAG( pcSPS->getUseStrongIntraSmoothing(),             "sps_strong_intra_smoothing_enable_flag" );
708
709  WRITE_FLAG( pcSPS->getVuiParametersPresentFlag(),             "vui_parameters_present_flag" );
710  if (pcSPS->getVuiParametersPresentFlag())
711  {
712      codeVUI(pcSPS->getVuiParameters(), pcSPS);
713  }
714
715  Bool sps_extension_present_flag=false;
716  Bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS]={false};
717
718  sps_extension_flags[SPS_EXT__REXT] = (
719          pcSPS->getUseResidualRotation()
720       || pcSPS->getUseSingleSignificanceMapContext()
721       || pcSPS->getUseResidualDPCM(RDPCM_SIGNAL_IMPLICIT)
722       || pcSPS->getUseResidualDPCM(RDPCM_SIGNAL_EXPLICIT)
723       || pcSPS->getUseExtendedPrecision()
724       || pcSPS->getDisableIntraReferenceSmoothing()
725       || pcSPS->getUseHighPrecisionPredictionWeighting()
726       || pcSPS->getUseGolombRiceParameterAdaptation()
727       || pcSPS->getAlignCABACBeforeBypass()
728    );
729
730  // Other SPS extension flags checked here.
731#if SVC_EXTENSION
732  sps_extension_flags[SPS_EXT__MLAYER] = pcSPS->getExtensionFlag() ? 1 : 0;
733#endif
734
735  for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
736  {
737    sps_extension_present_flag|=sps_extension_flags[i];
738  }
739
740  WRITE_FLAG( (sps_extension_present_flag?1:0), "sps_extension_present_flag" );
741
742  if (sps_extension_present_flag)
743  {
744    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
745    {
746      WRITE_FLAG( sps_extension_flags[i]?1:0, "sps_extension_flag[]" );
747    }
748
749    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
750    {
751      if (sps_extension_flags[i])
752      {
753        switch (SPSExtensionFlagIndex(i))
754        {
755          case SPS_EXT__REXT:
756
757            WRITE_FLAG( (pcSPS->getUseResidualRotation() ? 1 : 0),                  "transform_skip_rotation_enabled_flag");
758            WRITE_FLAG( (pcSPS->getUseSingleSignificanceMapContext() ? 1 : 0),      "transform_skip_context_enabled_flag");
759            WRITE_FLAG( (pcSPS->getUseResidualDPCM(RDPCM_SIGNAL_IMPLICIT) ? 1 : 0), "residual_dpcm_implicit_enabled_flag" );
760            WRITE_FLAG( (pcSPS->getUseResidualDPCM(RDPCM_SIGNAL_EXPLICIT) ? 1 : 0), "residual_dpcm_explicit_enabled_flag" );
761            WRITE_FLAG( (pcSPS->getUseExtendedPrecision() ? 1 : 0),                 "extended_precision_processing_flag" );
762            WRITE_FLAG( (pcSPS->getDisableIntraReferenceSmoothing() ? 1 : 0),       "intra_smoothing_disabled_flag" );
763            WRITE_FLAG( (pcSPS->getUseHighPrecisionPredictionWeighting() ? 1 : 0),  "high_precision_prediction_weighting_flag" );
764            WRITE_FLAG( (pcSPS->getUseGolombRiceParameterAdaptation() ? 1 : 0),     "golomb_rice_parameter_adaptation_flag" );
765            WRITE_FLAG( (pcSPS->getAlignCABACBeforeBypass() ? 1 : 0),               "cabac_bypass_alignment_enabled_flag" );
766            break;
767#if SVC_EXTENSION
768          case SPS_EXT__MLAYER:
769            codeSPSExtension( pcSPS ); //it is sps_multilayer_extension
770            break;
771#endif
772          default:
773            assert(sps_extension_flags[i]==false); // Should never get here with an active SPS extension flag.
774            break;
775        }
776      }
777    }
778  }
779}
780
781Void TEncCavlc::codeVPS( TComVPS* pcVPS )
782{
783  WRITE_CODE( pcVPS->getVPSId(),                    4,        "vps_video_parameter_set_id" );
784#if SVC_EXTENSION
785  WRITE_FLAG( pcVPS->getBaseLayerInternalFlag(),              "vps_base_layer_internal_flag");
786  WRITE_FLAG( pcVPS->getBaseLayerAvailableFlag(),             "vps_base_layer_available_flag");
787  WRITE_CODE( pcVPS->getMaxLayers() - 1,            6,        "vps_max_layers_minus1" );
788  assert( pcVPS->getBaseLayerInternalFlag() || pcVPS->getMaxLayers() > 1 );
789#else
790  WRITE_CODE( 3,                                    2,        "vps_reserved_three_2bits" );
791  WRITE_CODE( 0,                                    6,        "vps_reserved_zero_6bits" );
792#endif
793  WRITE_CODE( pcVPS->getMaxTLayers() - 1,           3,        "vps_max_sub_layers_minus1" );
794  WRITE_FLAG( pcVPS->getTemporalNestingFlag(),                "vps_temporal_id_nesting_flag" );
795  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
796  WRITE_CODE( 0xffff,                              16,        "vps_reserved_ffff_16bits" );
797  codePTL( pcVPS->getPTL(), true, pcVPS->getMaxTLayers() - 1 );
798  const Bool subLayerOrderingInfoPresentFlag = 1;
799  WRITE_FLAG(subLayerOrderingInfoPresentFlag,              "vps_sub_layer_ordering_info_present_flag");
800  for(UInt i=0; i <= pcVPS->getMaxTLayers()-1; i++)
801  {
802    WRITE_UVLC( pcVPS->getMaxDecPicBuffering(i) - 1,       "vps_max_dec_pic_buffering_minus1[i]" );
803    WRITE_UVLC( pcVPS->getNumReorderPics(i),               "vps_num_reorder_pics[i]" );
804    WRITE_UVLC( pcVPS->getMaxLatencyIncrease(i),           "vps_max_latency_increase_plus1[i]" );
805    if (!subLayerOrderingInfoPresentFlag)
806    {
807      break;
808    }
809  }
810
811#if SVC_EXTENSION
812  assert( pcVPS->getNumHrdParameters() <= MAX_VPS_LAYER_SETS_PLUS1 );
813  assert( pcVPS->getMaxLayerId() < MAX_VPS_LAYER_IDX_PLUS1 );
814
815  WRITE_CODE( pcVPS->getMaxLayerId(), 6,                       "vps_max_layer_id" );
816  WRITE_UVLC(pcVPS->getVpsNumLayerSetsMinus1(),                "vps_num_layer_sets_minus1");
817
818  for( UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx++ )
819  {
820    // Operation point set
821    for( UInt i = 0; i <= pcVPS->getMaxLayerId(); i ++ )
822    {
823#else
824  assert( pcVPS->getNumHrdParameters() <= MAX_VPS_NUM_HRD_PARAMETERS );
825  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
826  WRITE_CODE( pcVPS->getMaxNuhReservedZeroLayerId(), 6,     "vps_max_nuh_reserved_zero_layer_id" );
827  pcVPS->setMaxOpSets(1);
828  WRITE_UVLC( pcVPS->getMaxOpSets() - 1,                    "vps_max_op_sets_minus1" );
829  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
830  {
831    // Operation point set
832    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
833    {
834      // Only applicable for version 1
835      pcVPS->setLayerIdIncludedFlag( true, opsIdx, i );
836#endif
837      WRITE_FLAG( pcVPS->getLayerIdIncludedFlag( opsIdx, i ) ? 1 : 0, "layer_id_included_flag[opsIdx][i]" );
838    }
839  }
840
841  TimingInfo *timingInfo = pcVPS->getTimingInfo();
842  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vps_timing_info_present_flag");
843  if(timingInfo->getTimingInfoPresentFlag())
844  {
845    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vps_num_units_in_tick");
846    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vps_time_scale");
847    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vps_poc_proportional_to_timing_flag");
848    if(timingInfo->getPocProportionalToTimingFlag())
849    {
850      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vps_num_ticks_poc_diff_one_minus1");
851    }
852    pcVPS->setNumHrdParameters( 0 );
853    WRITE_UVLC( pcVPS->getNumHrdParameters(),                 "vps_num_hrd_parameters" );
854
855    if( pcVPS->getNumHrdParameters() > 0 )
856    {
857      pcVPS->createHrdParamBuffer();
858    }
859    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
860    {
861      // Only applicable for version 1
862      pcVPS->setHrdOpSetIdx( 0, i );
863      WRITE_UVLC( pcVPS->getHrdOpSetIdx( i ),                "hrd_op_set_idx" );
864      if( i > 0 )
865      {
866        WRITE_FLAG( pcVPS->getCprmsPresentFlag( i ) ? 1 : 0, "cprms_present_flag[i]" );
867      }
868      codeHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
869    }
870  }
871#if SVC_EXTENSION
872  // When MaxLayersMinus1 is greater than 0, vps_extension_flag shall be equal to 1.
873  if( pcVPS->getMaxLayers() > 1 )
874  {
875    assert( pcVPS->getVpsExtensionFlag() == true );
876  }
877
878  WRITE_FLAG( pcVPS->getVpsExtensionFlag() ? 1 : 0,                     "vps_extension_flag" );
879
880  if( pcVPS->getVpsExtensionFlag() )
881  {
882    while ( m_pcBitIf->getNumberOfWrittenBits() % 8 != 0 )
883    {
884      WRITE_FLAG(1,                  "vps_extension_alignment_bit_equal_to_one");
885    }
886    codeVPSExtension(pcVPS);
887    WRITE_FLAG( 0,                     "vps_extension2_flag" );   // Flag value of 1 reserved
888  }
889#else
890  WRITE_FLAG( 0,                     "vps_extension_flag" );
891#endif 
892  //future extensions here..
893 
894  return;
895}
896
897Void TEncCavlc::codeSliceHeader         ( TComSlice* pcSlice )
898{
899#if ENC_DEC_TRACE
900  xTraceSliceHeader (pcSlice);
901#endif
902
903  const ChromaFormat format                = pcSlice->getSPS()->getChromaFormatIdc();
904  const UInt         numberValidComponents = getNumberValidComponents(format);
905  const Bool         chromaEnabled         = isChromaEnabled(format);
906
907  //calculate number of bits required for slice address
908  Int maxSliceSegmentAddress = pcSlice->getPic()->getNumberOfCtusInFrame();
909  Int bitsSliceSegmentAddress = 0;
910  while(maxSliceSegmentAddress>(1<<bitsSliceSegmentAddress))
911  {
912    bitsSliceSegmentAddress++;
913  }
914  const Int ctuTsAddress = pcSlice->getSliceSegmentCurStartCtuTsAddr();
915
916  //write slice address
917  const Int sliceSegmentRsAddress = pcSlice->getPic()->getPicSym()->getCtuTsToRsAddrMap(ctuTsAddress);
918
919  WRITE_FLAG( sliceSegmentRsAddress==0, "first_slice_segment_in_pic_flag" );
920  if ( pcSlice->getRapPicFlag() )
921  {
922    WRITE_FLAG( pcSlice->getNoOutputPriorPicsFlag() ? 1 : 0, "no_output_of_prior_pics_flag" );
923  }
924  WRITE_UVLC( pcSlice->getPPS()->getPPSId(), "slice_pic_parameter_set_id" );
925  if ( pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag() && (sliceSegmentRsAddress!=0) )
926  {
927    WRITE_FLAG( pcSlice->getDependentSliceSegmentFlag() ? 1 : 0, "dependent_slice_segment_flag" );
928  }
929  if(sliceSegmentRsAddress>0)
930  {
931    WRITE_CODE( sliceSegmentRsAddress, bitsSliceSegmentAddress, "slice_segment_address" );
932  }
933  if ( !pcSlice->getDependentSliceSegmentFlag() )
934  {
935#if SVC_EXTENSION
936    Int iBits = 0;
937    if(pcSlice->getPPS()->getNumExtraSliceHeaderBits() > iBits)
938    {
939      assert(!!"discardable_flag");
940
941      if (pcSlice->getDiscardableFlag())
942      {
943        assert(pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
944          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
945          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
946          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
947          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
948      }
949
950      WRITE_FLAG(pcSlice->getDiscardableFlag(), "discardable_flag");
951      iBits++;
952    }
953
954    if( pcSlice->getPPS()->getNumExtraSliceHeaderBits() > iBits )
955    {
956      assert(!!"cross_layer_bla_flag");
957      WRITE_FLAG(pcSlice->getCrossLayerBLAFlag(), "cross_layer_bla_flag");
958      iBits++;
959    }
960
961    for (; iBits < pcSlice->getPPS()->getNumExtraSliceHeaderBits(); iBits++)
962    {
963      assert(!!"slice_reserved_undetermined_flag[]");
964      WRITE_FLAG(0, "slice_reserved_undetermined_flag[]");
965    }
966#else //SVC_EXTENSION
967    for (Int i = 0; i < pcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
968    {
969      assert(!!"slice_reserved_undetermined_flag[]");
970      WRITE_FLAG(0, "slice_reserved_undetermined_flag[]");
971    }
972#endif //SVC_EXTENSION
973
974    WRITE_UVLC( pcSlice->getSliceType(),       "slice_type" );
975
976    if( pcSlice->getPPS()->getOutputFlagPresentFlag() )
977    {
978      WRITE_FLAG( pcSlice->getPicOutputFlag() ? 1 : 0, "pic_output_flag" );
979    }
980
981#if SVC_EXTENSION
982    if( (pcSlice->getLayerId() > 0 && !pcSlice->getVPS()->getPocLsbNotPresentFlag( pcSlice->getVPS()->getLayerIdxInVps(pcSlice->getLayerId())) ) || !pcSlice->getIdrPicFlag())
983#else
984    if( !pcSlice->getIdrPicFlag() )
985#endif
986    {
987#if SVC_EXTENSION && POC_RESET_IDC_ENCODER
988      Int picOrderCntLSB;
989      if( pcSlice->getPocResetIdc() == 2 )  // i.e. the LSB is reset
990      {
991        picOrderCntLSB = pcSlice->getPicOrderCntLsb();  // This will be the LSB value w.r.t to the previous POC reset period.
992      }
993      else
994      {
995        picOrderCntLSB = (pcSlice->getPOC() + (1<<pcSlice->getSPS()->getBitsForPOC())) & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
996      }
997#else
998      Int picOrderCntLSB = (pcSlice->getPOC()-pcSlice->getLastIDR()+(1<<pcSlice->getSPS()->getBitsForPOC())) & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
999#endif
1000      WRITE_CODE( picOrderCntLSB, pcSlice->getSPS()->getBitsForPOC(), "pic_order_cnt_lsb");
1001
1002#if SVC_EXTENSION
1003    }
1004    if( !pcSlice->getIdrPicFlag() )
1005    {
1006#endif
1007      TComReferencePictureSet* rps = pcSlice->getRPS();
1008
1009      // check for bitstream restriction stating that:
1010      // If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
1011      // Ideally this process should not be repeated for each slice in a picture
1012#if SVC_EXTENSION
1013      if( pcSlice->getLayerId() == 0 )
1014#endif
1015      if (pcSlice->isIRAP())
1016      {
1017        for (Int picIdx = 0; picIdx < rps->getNumberOfPictures(); picIdx++)
1018        {
1019          assert (!rps->getUsed(picIdx));
1020        }
1021      }
1022
1023      if(pcSlice->getRPSidx() < 0)
1024      {
1025        WRITE_FLAG( 0, "short_term_ref_pic_set_sps_flag");
1026        codeShortTermRefPicSet(pcSlice->getSPS(), rps, true, pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets());
1027      }
1028      else
1029      {
1030        WRITE_FLAG( 1, "short_term_ref_pic_set_sps_flag");
1031        Int numBits = 0;
1032        while ((1 << numBits) < pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
1033        {
1034          numBits++;
1035        }
1036        if (numBits > 0)
1037        {
1038          WRITE_CODE( pcSlice->getRPSidx(), numBits, "short_term_ref_pic_set_idx" );
1039        }
1040      }
1041      if(pcSlice->getSPS()->getLongTermRefsPresent())
1042      {
1043        Int numLtrpInSH = rps->getNumberOfLongtermPictures();
1044        Int ltrpInSPS[MAX_NUM_REF_PICS];
1045        Int numLtrpInSPS = 0;
1046        UInt ltrpIndex;
1047        Int counter = 0;
1048        for(Int k = rps->getNumberOfPictures()-1; k > rps->getNumberOfPictures()-rps->getNumberOfLongtermPictures()-1; k--)
1049        {
1050          if (findMatchingLTRP(pcSlice, &ltrpIndex, rps->getPOC(k), rps->getUsed(k)))
1051          {
1052            ltrpInSPS[numLtrpInSPS] = ltrpIndex;
1053            numLtrpInSPS++;
1054          }
1055          else
1056          {
1057            counter++;
1058          }
1059        }
1060        numLtrpInSH -= numLtrpInSPS;
1061
1062        Int bitsForLtrpInSPS = 0;
1063        while (pcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
1064        {
1065          bitsForLtrpInSPS++;
1066        }
1067        if (pcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
1068        {
1069          WRITE_UVLC( numLtrpInSPS, "num_long_term_sps");
1070        }
1071        WRITE_UVLC( numLtrpInSH, "num_long_term_pics");
1072        // Note that the LSBs of the LT ref. pic. POCs must be sorted before.
1073        // Not sorted here because LT ref indices will be used in setRefPicList()
1074        Int prevDeltaMSB = 0, prevLSB = 0;
1075        Int offset = rps->getNumberOfNegativePictures() + rps->getNumberOfPositivePictures();
1076        for(Int i=rps->getNumberOfPictures()-1 ; i > offset-1; i--)
1077        {
1078          if (counter < numLtrpInSPS)
1079          {
1080            if (bitsForLtrpInSPS > 0)
1081            {
1082              WRITE_CODE( ltrpInSPS[counter], bitsForLtrpInSPS, "lt_idx_sps[i]");
1083            }
1084          }
1085          else
1086          {
1087            WRITE_CODE( rps->getPocLSBLT(i), pcSlice->getSPS()->getBitsForPOC(), "poc_lsb_lt");
1088            WRITE_FLAG( rps->getUsed(i), "used_by_curr_pic_lt_flag");
1089          }
1090          WRITE_FLAG( rps->getDeltaPocMSBPresentFlag(i), "delta_poc_msb_present_flag");
1091
1092          if(rps->getDeltaPocMSBPresentFlag(i))
1093          {
1094            Bool deltaFlag = false;
1095            //  First LTRP from SPS                 ||  First LTRP from SH                              || curr LSB            != prev LSB
1096            if( (i == rps->getNumberOfPictures()-1) || (i == rps->getNumberOfPictures()-1-numLtrpInSPS) || (rps->getPocLSBLT(i) != prevLSB) )
1097            {
1098              deltaFlag = true;
1099            }
1100            if(deltaFlag)
1101            {
1102              WRITE_UVLC( rps->getDeltaPocMSBCycleLT(i), "delta_poc_msb_cycle_lt[i]" );
1103            }
1104            else
1105            {
1106              Int differenceInDeltaMSB = rps->getDeltaPocMSBCycleLT(i) - prevDeltaMSB;
1107              assert(differenceInDeltaMSB >= 0);
1108              WRITE_UVLC( differenceInDeltaMSB, "delta_poc_msb_cycle_lt[i]" );
1109            }
1110            prevLSB = rps->getPocLSBLT(i);
1111            prevDeltaMSB = rps->getDeltaPocMSBCycleLT(i);
1112          }
1113        }
1114      }
1115      if (pcSlice->getSPS()->getTMVPFlagsPresent())
1116      {
1117        WRITE_FLAG( pcSlice->getEnableTMVPFlag() ? 1 : 0, "slice_temporal_mvp_enabled_flag" );
1118      }
1119    }
1120
1121#if SVC_EXTENSION
1122    if((pcSlice->getLayerId() > 0) && !(pcSlice->getVPS()->getIlpSshSignalingEnabledFlag()) && (pcSlice->getNumILRRefIdx() > 0) )
1123    {
1124      WRITE_FLAG(pcSlice->getInterLayerPredEnabledFlag(),"inter_layer_pred_enabled_flag");
1125      if( pcSlice->getInterLayerPredEnabledFlag())
1126      {
1127        if(pcSlice->getNumILRRefIdx() > 1)
1128        {
1129          Int numBits = 1;
1130          while ((1 << numBits) < pcSlice->getNumILRRefIdx())
1131          {
1132            numBits++;
1133          }
1134          if( !pcSlice->getVPS()->getMaxOneActiveRefLayerFlag()) 
1135          {
1136            WRITE_CODE(pcSlice->getActiveNumILRRefIdx() - 1, numBits,"num_inter_layer_ref_pics_minus1");
1137          }       
1138
1139          if( pcSlice->getNumILRRefIdx() != pcSlice->getActiveNumILRRefIdx() )
1140          {
1141            for(Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1142            {
1143              WRITE_CODE(pcSlice->getInterLayerPredLayerIdc(i),numBits,"inter_layer_pred_layer_idc[i]");   
1144            }
1145          }
1146        }
1147      }
1148    }     
1149#endif //SVC_EXTENSION
1150
1151    if(pcSlice->getSPS()->getUseSAO())
1152    {
1153       WRITE_FLAG( pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA), "slice_sao_luma_flag" );
1154       if (chromaEnabled) WRITE_FLAG( pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA), "slice_sao_chroma_flag" );
1155    }
1156
1157    //check if numrefidxes match the defaults. If not, override
1158
1159    if (!pcSlice->isIntra())
1160    {
1161      Bool overrideFlag = (pcSlice->getNumRefIdx( REF_PIC_LIST_0 )!=pcSlice->getPPS()->getNumRefIdxL0DefaultActive()||(pcSlice->isInterB()&&pcSlice->getNumRefIdx( REF_PIC_LIST_1 )!=pcSlice->getPPS()->getNumRefIdxL1DefaultActive()));
1162      WRITE_FLAG( overrideFlag ? 1 : 0,                               "num_ref_idx_active_override_flag");
1163      if (overrideFlag)
1164      {
1165        WRITE_UVLC( pcSlice->getNumRefIdx( REF_PIC_LIST_0 ) - 1,      "num_ref_idx_l0_active_minus1" );
1166        if (pcSlice->isInterB())
1167        {
1168          WRITE_UVLC( pcSlice->getNumRefIdx( REF_PIC_LIST_1 ) - 1,    "num_ref_idx_l1_active_minus1" );
1169        }
1170        else
1171        {
1172          pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
1173        }
1174      }
1175    }
1176    else
1177    {
1178      pcSlice->setNumRefIdx(REF_PIC_LIST_0, 0);
1179      pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
1180    }
1181
1182    if( pcSlice->getPPS()->getListsModificationPresentFlag() && pcSlice->getNumRpsCurrTempList() > 1)
1183    {
1184      TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
1185      if(!pcSlice->isIntra())
1186      {
1187        WRITE_FLAG(pcSlice->getRefPicListModification()->getRefPicListModificationFlagL0() ? 1 : 0,       "ref_pic_list_modification_flag_l0" );
1188        if (pcSlice->getRefPicListModification()->getRefPicListModificationFlagL0())
1189        {
1190          Int numRpsCurrTempList0 = pcSlice->getNumRpsCurrTempList();
1191          if (numRpsCurrTempList0 > 1)
1192          {
1193            Int length = 1;
1194            numRpsCurrTempList0 --;
1195            while ( numRpsCurrTempList0 >>= 1)
1196            {
1197              length ++;
1198            }
1199            for(Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_0 ); i++)
1200            {
1201              WRITE_CODE( refPicListModification->getRefPicSetIdxL0(i), length, "list_entry_l0");
1202            }
1203          }
1204        }
1205      }
1206      if(pcSlice->isInterB())
1207      {
1208        WRITE_FLAG(pcSlice->getRefPicListModification()->getRefPicListModificationFlagL1() ? 1 : 0,       "ref_pic_list_modification_flag_l1" );
1209        if (pcSlice->getRefPicListModification()->getRefPicListModificationFlagL1())
1210        {
1211          Int numRpsCurrTempList1 = pcSlice->getNumRpsCurrTempList();
1212          if ( numRpsCurrTempList1 > 1 )
1213          {
1214            Int length = 1;
1215            numRpsCurrTempList1 --;
1216            while ( numRpsCurrTempList1 >>= 1)
1217            {
1218              length ++;
1219            }
1220            for(Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_1 ); i++)
1221            {
1222              WRITE_CODE( refPicListModification->getRefPicSetIdxL1(i), length, "list_entry_l1");
1223            }
1224          }
1225        }
1226      }
1227    }
1228
1229    if (pcSlice->isInterB())
1230    {
1231      WRITE_FLAG( pcSlice->getMvdL1ZeroFlag() ? 1 : 0,   "mvd_l1_zero_flag");
1232    }
1233
1234    if(!pcSlice->isIntra())
1235    {
1236      if (!pcSlice->isIntra() && pcSlice->getPPS()->getCabacInitPresentFlag())
1237      {
1238        SliceType sliceType   = pcSlice->getSliceType();
1239        Int  encCABACTableIdx = pcSlice->getPPS()->getEncCABACTableIdx();
1240        Bool encCabacInitFlag = (sliceType!=encCABACTableIdx && encCABACTableIdx!=I_SLICE) ? true : false;
1241        pcSlice->setCabacInitFlag( encCabacInitFlag );
1242        WRITE_FLAG( encCabacInitFlag?1:0, "cabac_init_flag" );
1243      }
1244    }
1245
1246    if ( pcSlice->getEnableTMVPFlag() )
1247    {
1248      if ( pcSlice->getSliceType() == B_SLICE )
1249      {
1250        WRITE_FLAG( pcSlice->getColFromL0Flag(), "collocated_from_l0_flag" );
1251      }
1252
1253      if ( pcSlice->getSliceType() != I_SLICE &&
1254        ((pcSlice->getColFromL0Flag()==1 && pcSlice->getNumRefIdx(REF_PIC_LIST_0)>1)||
1255        (pcSlice->getColFromL0Flag()==0  && pcSlice->getNumRefIdx(REF_PIC_LIST_1)>1)))
1256      {
1257        WRITE_UVLC( pcSlice->getColRefIdx(), "collocated_ref_idx" );
1258      }
1259    }
1260    if ( (pcSlice->getPPS()->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pcSlice->getPPS()->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
1261    {
1262      xCodePredWeightTable( pcSlice );
1263    }
1264    assert(pcSlice->getMaxNumMergeCand()<=MRG_MAX_NUM_CANDS);
1265    if (!pcSlice->isIntra())
1266    {
1267      WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
1268    }
1269    Int iCode = pcSlice->getSliceQp() - ( pcSlice->getPPS()->getPicInitQPMinus26() + 26 );
1270    WRITE_SVLC( iCode, "slice_qp_delta" );
1271    if (pcSlice->getPPS()->getSliceChromaQpFlag())
1272    {
1273      if (numberValidComponents > COMPONENT_Cb) { WRITE_SVLC( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb), "slice_qp_delta_cb" ); }
1274      if (numberValidComponents > COMPONENT_Cr) { WRITE_SVLC( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr), "slice_qp_delta_cr" ); }
1275      assert(numberValidComponents <= COMPONENT_Cr+1);
1276    }
1277
1278    if (pcSlice->getPPS()->getChromaQpAdjTableSize() > 0)
1279    {
1280      WRITE_FLAG(pcSlice->getUseChromaQpAdj(), "slice_chroma_qp_adjustment_enabled_flag");
1281    }
1282
1283    if (pcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
1284    {
1285      if (pcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag() )
1286      {
1287        WRITE_FLAG(pcSlice->getDeblockingFilterOverrideFlag(), "deblocking_filter_override_flag");
1288      }
1289      if (pcSlice->getDeblockingFilterOverrideFlag())
1290      {
1291        WRITE_FLAG(pcSlice->getDeblockingFilterDisable(), "slice_disable_deblocking_filter_flag");
1292        if(!pcSlice->getDeblockingFilterDisable())
1293        {
1294          WRITE_SVLC (pcSlice->getDeblockingFilterBetaOffsetDiv2(), "slice_beta_offset_div2");
1295          WRITE_SVLC (pcSlice->getDeblockingFilterTcOffsetDiv2(),   "slice_tc_offset_div2");
1296        }
1297      }
1298    }
1299
1300    Bool isSAOEnabled = pcSlice->getSPS()->getUseSAO() && (pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA) || (chromaEnabled && pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA)));
1301    Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
1302
1303    if(pcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
1304    {
1305      WRITE_FLAG(pcSlice->getLFCrossSliceBoundaryFlag()?1:0, "slice_loop_filter_across_slices_enabled_flag");
1306    }
1307  }
1308
1309#if !POC_RESET_IDC_SIGNALLING   // Wrong place to put slice header extension
1310  if(pcSlice->getPPS()->getSliceHeaderExtensionPresentFlag())
1311  {
1312    WRITE_UVLC(0,"slice_header_extension_length");
1313  }
1314#endif
1315}
1316
1317Void TEncCavlc::codePTL( TComPTL* pcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1)
1318{
1319  if(profilePresentFlag)
1320  {
1321    codeProfileTier(pcPTL->getGeneralPTL());    // general_...
1322  }
1323  WRITE_CODE( Int(pcPTL->getGeneralPTL()->getLevelIdc()), 8, "general_level_idc" );
1324
1325  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
1326  {
1327#if SVC_EXTENSION
1328    WRITE_FLAG( pcPTL->getSubLayerProfilePresentFlag(i), "sub_layer_profile_present_flag[i]" );
1329#else
1330    if(profilePresentFlag)
1331    {
1332      WRITE_FLAG( pcPTL->getSubLayerProfilePresentFlag(i), "sub_layer_profile_present_flag[i]" );
1333    }
1334#endif
1335
1336    WRITE_FLAG( pcPTL->getSubLayerLevelPresentFlag(i),   "sub_layer_level_present_flag[i]" );
1337  }
1338
1339  if (maxNumSubLayersMinus1 > 0)
1340  {
1341    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
1342    {
1343      WRITE_CODE(0, 2, "reserved_zero_2bits");
1344    }
1345  }
1346
1347  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
1348  {
1349#if SVC_EXTENSION
1350    if( pcPTL->getSubLayerProfilePresentFlag(i) )
1351#else
1352    if( profilePresentFlag && pcPTL->getSubLayerProfilePresentFlag(i) )
1353#endif
1354    {
1355      codeProfileTier(pcPTL->getSubLayerPTL(i));  // sub_layer_...
1356    }
1357    if( pcPTL->getSubLayerLevelPresentFlag(i) )
1358    {
1359      WRITE_CODE( Int(pcPTL->getSubLayerPTL(i)->getLevelIdc()), 8, "sub_layer_level_idc[i]" );
1360    }
1361  }
1362}
1363Void TEncCavlc::codeProfileTier( ProfileTierLevel* ptl )
1364{
1365  WRITE_CODE( ptl->getProfileSpace(), 2 ,     "XXX_profile_space[]");
1366  WRITE_FLAG( ptl->getTierFlag()==Level::HIGH, "XXX_tier_flag[]"    );
1367#if SVC_EXTENSION
1368  WRITE_CODE( (ptl->getProfileIdc() == Profile::SCALABLEMAIN || ptl->getProfileIdc() == Profile::SCALABLEMAIN10) ? 7 : Int(ptl->getProfileIdc()), 5 ,  "XXX_profile_idc[]"  );
1369#else
1370  WRITE_CODE( Int(ptl->getProfileIdc()), 5 ,  "XXX_profile_idc[]"  );
1371#endif
1372  for(Int j = 0; j < 32; j++)
1373  {
1374    WRITE_FLAG( ptl->getProfileCompatibilityFlag(j), "XXX_profile_compatibility_flag[][j]");
1375  }
1376
1377  WRITE_FLAG(ptl->getProgressiveSourceFlag(),   "general_progressive_source_flag");
1378  WRITE_FLAG(ptl->getInterlacedSourceFlag(),    "general_interlaced_source_flag");
1379  WRITE_FLAG(ptl->getNonPackedConstraintFlag(), "general_non_packed_constraint_flag");
1380  WRITE_FLAG(ptl->getFrameOnlyConstraintFlag(), "general_frame_only_constraint_flag");
1381
1382  if (ptl->getProfileIdc() == Profile::MAINREXT || ptl->getProfileIdc() == Profile::HIGHTHROUGHPUTREXT )
1383  {
1384    const UInt         bitDepthConstraint=ptl->getBitDepthConstraint();
1385    WRITE_FLAG(bitDepthConstraint<=12, "general_max_12bit_constraint_flag");
1386    WRITE_FLAG(bitDepthConstraint<=10, "general_max_10bit_constraint_flag");
1387    WRITE_FLAG(bitDepthConstraint<= 8, "general_max_8bit_constraint_flag");
1388    const ChromaFormat chromaFmtConstraint=ptl->getChromaFormatConstraint();
1389    WRITE_FLAG(chromaFmtConstraint==CHROMA_422||chromaFmtConstraint==CHROMA_420||chromaFmtConstraint==CHROMA_400, "general_max_422chroma_constraint_flag");
1390    WRITE_FLAG(chromaFmtConstraint==CHROMA_420||chromaFmtConstraint==CHROMA_400,                                  "general_max_420chroma_constraint_flag");
1391    WRITE_FLAG(chromaFmtConstraint==CHROMA_400,                                                                   "general_max_monochrome_constraint_flag");
1392    WRITE_FLAG(ptl->getIntraConstraintFlag(),        "general_intra_constraint_flag");
1393    WRITE_FLAG(0,                                    "general_one_picture_only_constraint_flag");
1394    WRITE_FLAG(ptl->getLowerBitRateConstraintFlag(), "general_lower_bit_rate_constraint_flag");
1395#if SVC_EXTENSION
1396    WRITE_CODE(0, 32,  "general_reserved_zero_34bits");  WRITE_CODE(0, 2,  "general_reserved_zero_34bits");
1397  }
1398  else if( ptl->getProfileIdc() == Profile::SCALABLEMAIN || ptl->getProfileIdc() == Profile::SCALABLEMAIN10 )      // at encoder side, scalable-main10 profile has a profile idc equal to 8, which is converted to 7 during signalling
1399  {
1400    WRITE_FLAG(true,   "general_max_12bit_constraint_flag");
1401    WRITE_FLAG(true,   "general_max_10bit_constraint_flag");
1402    WRITE_FLAG((ptl->getProfileIdc() == Profile::SCALABLEMAIN) ? true : false, "general_max_8bit_constraint_flag");
1403    WRITE_FLAG(true,   "general_max_422chroma_constraint_flag");
1404    WRITE_FLAG(true,   "general_max_420chroma_constraint_flag");
1405    WRITE_FLAG(false,  "general_max_monochrome_constraint_flag");
1406    WRITE_FLAG(false,  "general_intra_constraint_flag");
1407    WRITE_FLAG(false,  "general_one_picture_only_constraint_flag");
1408    WRITE_FLAG(true,   "general_lower_bit_rate_constraint_flag");
1409    WRITE_CODE(0, 32,  "general_reserved_zero_34bits");  WRITE_CODE(0, 2,  "general_reserved_zero_34bits");
1410  }
1411  else
1412  {
1413    WRITE_CODE(0, 32,  "general_reserved_zero_43bits");  WRITE_CODE(0, 11,  "general_reserved_zero_43bits");
1414  }
1415
1416  if( ( ptl->getProfileIdc() >= 1 && ptl->getProfileIdc() <= 5 ) || 
1417      ptl->getProfileCompatibilityFlag(1) || ptl->getProfileCompatibilityFlag(2) || 
1418      ptl->getProfileCompatibilityFlag(3) || ptl->getProfileCompatibilityFlag(4) || 
1419      ptl->getProfileCompatibilityFlag(5)                                           )
1420  {
1421    WRITE_FLAG(false, "general_inbld_flag");
1422  }
1423  else
1424  {
1425    WRITE_FLAG(false, "general_reserved_zero_bit");
1426  }
1427#else
1428    WRITE_CODE(0 , 16, "XXX_reserved_zero_35bits[0..15]");
1429    WRITE_CODE(0 , 16, "XXX_reserved_zero_35bits[16..31]");
1430    WRITE_CODE(0 ,  3, "XXX_reserved_zero_35bits[32..34]");
1431  }
1432  else
1433  {
1434    WRITE_CODE(0x0000 , 16, "XXX_reserved_zero_44bits[0..15]");
1435    WRITE_CODE(0x0000 , 16, "XXX_reserved_zero_44bits[16..31]");
1436    WRITE_CODE(0x000  , 12, "XXX_reserved_zero_44bits[32..43]");
1437  }
1438#endif
1439}
1440
1441/**
1442 - write tiles and wavefront substreams sizes for the slice header.
1443 .
1444 \param pcSlice Where we find the substream size information.
1445 */
1446Void  TEncCavlc::codeTilesWPPEntryPoint( TComSlice* pSlice )
1447{
1448  if (!pSlice->getPPS()->getTilesEnabledFlag() && !pSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
1449  {
1450    return;
1451  }
1452  UInt maxOffset = 0;
1453  for(Int idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
1454  {
1455    UInt offset=pSlice->getSubstreamSize(idx);
1456    if ( offset > maxOffset )
1457    {
1458      maxOffset = offset;
1459    }
1460  }
1461
1462  // Determine number of bits "offsetLenMinus1+1" required for entry point information
1463  UInt offsetLenMinus1 = 0;
1464  while (maxOffset >= (1u << (offsetLenMinus1 + 1)))
1465  {
1466    offsetLenMinus1++;
1467    assert(offsetLenMinus1 + 1 < 32);
1468  }
1469
1470  WRITE_UVLC(pSlice->getNumberOfSubstreamSizes(), "num_entry_point_offsets");
1471  if (pSlice->getNumberOfSubstreamSizes()>0)
1472  {
1473    WRITE_UVLC(offsetLenMinus1, "offset_len_minus1");
1474
1475    for (UInt idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
1476    {
1477      WRITE_CODE(pSlice->getSubstreamSize(idx)-1, offsetLenMinus1+1, "entry_point_offset_minus1");
1478    }
1479  }
1480}
1481
1482Void TEncCavlc::codeTerminatingBit      ( UInt uilsLast )
1483{
1484}
1485
1486Void TEncCavlc::codeSliceFinish ()
1487{
1488}
1489
1490Void TEncCavlc::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1491{
1492  assert(0);
1493}
1494
1495Void TEncCavlc::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1496{
1497  assert(0);
1498}
1499
1500Void TEncCavlc::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
1501{
1502  assert(0);
1503}
1504
1505Void TEncCavlc::codeMergeFlag    ( TComDataCU* pcCU, UInt uiAbsPartIdx )
1506{
1507  assert(0);
1508}
1509
1510Void TEncCavlc::codeMergeIndex    ( TComDataCU* pcCU, UInt uiAbsPartIdx )
1511{
1512  assert(0);
1513}
1514
1515Void TEncCavlc::codeInterModeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiEncMode )
1516{
1517  assert(0);
1518}
1519
1520Void TEncCavlc::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
1521{
1522  assert(0);
1523}
1524
1525Void TEncCavlc::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
1526{
1527  assert(0);
1528}
1529
1530Void TEncCavlc::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1531{
1532  assert(0);
1533}
1534
1535Void TEncCavlc::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1536{
1537  assert(0);
1538}
1539
1540Void TEncCavlc::codeQtCbf( TComTU &rTu, const ComponentID compID, const Bool lowestLevel )
1541{
1542  assert(0);
1543}
1544
1545Void TEncCavlc::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1546{
1547  assert(0);
1548}
1549
1550Void TEncCavlc::codeQtCbfZero( TComTU &rTu, const ChannelType chType )
1551{
1552  assert(0);
1553}
1554Void TEncCavlc::codeQtRootCbfZero( TComDataCU* pcCU )
1555{
1556  assert(0);
1557}
1558
1559Void TEncCavlc::codeTransformSkipFlags (TComTU &rTu, ComponentID component )
1560{
1561  assert(0);
1562}
1563
1564/** Code I_PCM information.
1565 * \param pcCU pointer to CU
1566 * \param uiAbsPartIdx CU index
1567 * \returns Void
1568 */
1569Void TEncCavlc::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1570{
1571  assert(0);
1572}
1573
1574Void TEncCavlc::codeIntraDirLumaAng( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool isMultiple)
1575{
1576  assert(0);
1577}
1578
1579Void TEncCavlc::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1580{
1581  assert(0);
1582}
1583
1584Void TEncCavlc::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1585{
1586  assert(0);
1587}
1588
1589Void TEncCavlc::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1590{
1591  assert(0);
1592}
1593
1594Void TEncCavlc::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1595{
1596  assert(0);
1597}
1598
1599Void TEncCavlc::codeCrossComponentPrediction( TComTU& /*rTu*/, ComponentID /*compID*/ )
1600{
1601  assert(0);
1602}
1603
1604Void TEncCavlc::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1605{
1606  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1607
1608#if SVC_EXTENSION
1609  Int qpBdOffsetY =  pcCU->getSlice()->getQpBDOffsetY();
1610#else
1611  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
1612#endif
1613  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1614
1615  xWriteSvlc( iDQp );
1616
1617  return;
1618}
1619
1620Void TEncCavlc::codeChromaQpAdjustment( TComDataCU* pcCU, UInt uiAbsPartIdx )
1621{
1622  assert(0);
1623}
1624
1625Void TEncCavlc::codeCoeffNxN    ( TComTU &rTu, TCoeff* pcCoef, const ComponentID compID )
1626{
1627  assert(0);
1628}
1629
1630Void TEncCavlc::estBit( estBitsSbacStruct* pcEstBitsCabac, Int width, Int height, ChannelType chType )
1631{
1632  // printf("error : no VLC mode support in this version\n");
1633  return;
1634}
1635
1636// ====================================================================================================================
1637// Protected member functions
1638// ====================================================================================================================
1639
1640/** code explicit wp tables
1641 * \param TComSlice* pcSlice
1642 * \returns Void
1643 */
1644Void TEncCavlc::xCodePredWeightTable( TComSlice* pcSlice )
1645{
1646  WPScalingParam  *wp;
1647  const ChromaFormat    format                = pcSlice->getPic()->getChromaFormat();
1648  const UInt            numberValidComponents = getNumberValidComponents(format);
1649  const Bool            bChroma               = isChromaEnabled(format);
1650  const Int             iNbRef                = (pcSlice->getSliceType() == B_SLICE ) ? (2) : (1);
1651        Bool            bDenomCoded           = false;
1652        UInt            uiMode                = 0;
1653        UInt            uiTotalSignalledWeightFlags = 0;
1654
1655  if ( (pcSlice->getSliceType()==P_SLICE && pcSlice->getPPS()->getUseWP()) || (pcSlice->getSliceType()==B_SLICE && pcSlice->getPPS()->getWPBiPred()) )
1656  {
1657    uiMode = 1; // explicit
1658  }
1659  if(uiMode == 1)
1660  {
1661    for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ )
1662    {
1663      RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
1664
1665      // NOTE: wp[].uiLog2WeightDenom and wp[].bPresentFlag are actually per-channel-type settings.
1666
1667      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
1668      {
1669        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
1670        if ( !bDenomCoded )
1671        {
1672          Int iDeltaDenom;
1673          WRITE_UVLC( wp[COMPONENT_Y].uiLog2WeightDenom, "luma_log2_weight_denom" );     // ue(v): luma_log2_weight_denom
1674
1675          if( bChroma )
1676          {
1677            assert(wp[COMPONENT_Cb].uiLog2WeightDenom == wp[COMPONENT_Cr].uiLog2WeightDenom); // check the channel-type settings are consistent across components.
1678            iDeltaDenom = (wp[COMPONENT_Cb].uiLog2WeightDenom - wp[COMPONENT_Y].uiLog2WeightDenom);
1679            WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );       // se(v): delta_chroma_log2_weight_denom
1680          }
1681          bDenomCoded = true;
1682        }
1683        WRITE_FLAG( wp[COMPONENT_Y].bPresentFlag, "luma_weight_lX_flag" );               // u(1): luma_weight_lX_flag
1684        uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
1685      }
1686      if (bChroma)
1687      {
1688        for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
1689        {
1690          pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
1691          assert(wp[COMPONENT_Cb].bPresentFlag == wp[COMPONENT_Cr].bPresentFlag); // check the channel-type settings are consistent across components.
1692          WRITE_FLAG( wp[COMPONENT_Cb].bPresentFlag, "chroma_weight_lX_flag" );           // u(1): chroma_weight_lX_flag
1693          uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
1694        }
1695      }
1696
1697      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
1698      {
1699        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
1700        if ( wp[COMPONENT_Y].bPresentFlag )
1701        {
1702          Int iDeltaWeight = (wp[COMPONENT_Y].iWeight - (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
1703          WRITE_SVLC( iDeltaWeight, "delta_luma_weight_lX" );                            // se(v): delta_luma_weight_lX
1704          WRITE_SVLC( wp[COMPONENT_Y].iOffset, "luma_offset_lX" );                       // se(v): luma_offset_lX
1705        }
1706
1707        if ( bChroma )
1708        {
1709          if ( wp[COMPONENT_Cb].bPresentFlag )
1710          {
1711            for ( Int j = COMPONENT_Cb ; j < numberValidComponents ; j++ )
1712            {
1713              assert(wp[COMPONENT_Cb].uiLog2WeightDenom == wp[COMPONENT_Cr].uiLog2WeightDenom);
1714              Int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMPONENT_Cb].uiLog2WeightDenom));
1715              WRITE_SVLC( iDeltaWeight, "delta_chroma_weight_lX" );            // se(v): delta_chroma_weight_lX
1716
1717              Int range=pcSlice->getSPS()->getUseHighPrecisionPredictionWeighting() ? (1<<g_bitDepth[CHANNEL_TYPE_CHROMA])/2 : 128;
1718              Int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
1719              Int iDeltaChroma = (wp[j].iOffset - pred);
1720              WRITE_SVLC( iDeltaChroma, "delta_chroma_offset_lX" );            // se(v): delta_chroma_offset_lX
1721            }
1722          }
1723        }
1724      }
1725    }
1726    assert(uiTotalSignalledWeightFlags<=24);
1727  }
1728}
1729
1730/** code quantization matrix
1731 *  \param scalingList quantization matrix information
1732 */
1733Void TEncCavlc::codeScalingList( TComScalingList* scalingList )
1734{
1735  UInt listId,sizeId;
1736  Bool scalingListPredModeFlag;
1737
1738  //for each size
1739  for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
1740  {
1741    Int predListStep = (sizeId == SCALING_LIST_32x32? (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) : 1); // if 32x32, skip over chroma entries.
1742
1743    for(listId = 0; listId < SCALING_LIST_NUM; listId+=predListStep)
1744    {
1745      scalingListPredModeFlag = scalingList->checkPredMode( sizeId, listId );
1746      WRITE_FLAG( scalingListPredModeFlag, "scaling_list_pred_mode_flag" );
1747      if(!scalingListPredModeFlag)// Copy Mode
1748      {
1749        if (sizeId == SCALING_LIST_32x32)
1750        {
1751          // adjust the code, to cope with the missing chroma entries
1752          WRITE_UVLC( ((Int)listId - (Int)scalingList->getRefMatrixId (sizeId,listId)) / (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES), "scaling_list_pred_matrix_id_delta");
1753        }
1754        else
1755        {
1756          WRITE_UVLC( (Int)listId - (Int)scalingList->getRefMatrixId (sizeId,listId), "scaling_list_pred_matrix_id_delta");
1757        }
1758      }
1759      else// DPCM Mode
1760      {
1761        xCodeScalingList(scalingList, sizeId, listId);
1762      }
1763    }
1764  }
1765  return;
1766}
1767/** code DPCM
1768 * \param scalingList quantization matrix information
1769 * \param sizeIdc size index
1770 * \param listIdc list index
1771 */
1772Void TEncCavlc::xCodeScalingList(TComScalingList* scalingList, UInt sizeId, UInt listId)
1773{
1774  Int coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
1775  UInt* scan  = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId==0 ? 2 : 3][sizeId==0 ? 2 : 3];
1776  Int nextCoef = SCALING_LIST_START_VALUE;
1777  Int data;
1778  Int *src = scalingList->getScalingListAddress(sizeId, listId);
1779    if( sizeId > SCALING_LIST_8x8 )
1780    {
1781      WRITE_SVLC( scalingList->getScalingListDC(sizeId,listId) - 8, "scaling_list_dc_coef_minus8");
1782      nextCoef = scalingList->getScalingListDC(sizeId,listId);
1783    }
1784    for(Int i=0;i<coefNum;i++)
1785    {
1786      data = src[scan[i]] - nextCoef;
1787      nextCoef = src[scan[i]];
1788      if(data > 127)
1789      {
1790        data = data - 256;
1791      }
1792      if(data < -128)
1793      {
1794        data = data + 256;
1795      }
1796
1797      WRITE_SVLC( data,  "scaling_list_delta_coef");
1798    }
1799}
1800Bool TEncCavlc::findMatchingLTRP ( TComSlice* pcSlice, UInt *ltrpsIndex, Int ltrpPOC, Bool usedFlag )
1801{
1802  // Bool state = true, state2 = false;
1803  Int lsb = ltrpPOC & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
1804  for (Int k = 0; k < pcSlice->getSPS()->getNumLongTermRefPicSPS(); k++)
1805  {
1806    if ( (lsb == pcSlice->getSPS()->getLtRefPicPocLsbSps(k)) && (usedFlag == pcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(k)) )
1807    {
1808      *ltrpsIndex = k;
1809      return true;
1810    }
1811  }
1812  return false;
1813}
1814Bool TComScalingList::checkPredMode(UInt sizeId, UInt listId)
1815{
1816  Int predListStep = (sizeId == SCALING_LIST_32x32? (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) : 1); // if 32x32, skip over chroma entries.
1817
1818  for(Int predListIdx = (Int)listId ; predListIdx >= 0; predListIdx-=predListStep)
1819  {
1820    if( !memcmp(getScalingListAddress(sizeId,listId),((listId == predListIdx) ?
1821      getScalingListDefaultAddress(sizeId, predListIdx): getScalingListAddress(sizeId, predListIdx)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])) // check value of matrix
1822     && ((sizeId < SCALING_LIST_16x16) || (getScalingListDC(sizeId,listId) == getScalingListDC(sizeId,predListIdx)))) // check DC value
1823    {
1824      setRefMatrixId(sizeId, listId, predListIdx);
1825      return false;
1826    }
1827  }
1828  return true;
1829}
1830
1831Void TEncCavlc::codeExplicitRdpcmMode( TComTU &rTu, const ComponentID compID )
1832 {
1833   assert(0);
1834 }
1835
1836#if SVC_EXTENSION
1837
1838#if POC_RESET_IDC_SIGNALLING
1839Void  TEncCavlc::codeSliceHeaderExtn( TComSlice* slice, Int shBitsWrittenTillNow )
1840{
1841  Int tmpBitsBeforeWriting = getNumberOfWrittenBits();
1842  Int maxPocLsb = 1 << slice->getSPS()->getBitsForPOC();
1843  if(slice->getPPS()->getSliceHeaderExtensionPresentFlag())
1844  {
1845    // Derive the value of PocMsbValRequiredFlag
1846    slice->setPocMsbValRequiredFlag( (slice->getCraPicFlag() || slice->getBlaPicFlag())
1847                                  && (!slice->getVPS()->getVpsPocLsbAlignedFlag() ||
1848                                      (slice->getVPS()->getVpsPocLsbAlignedFlag() && slice->getVPS()->getNumDirectRefLayers(slice->getLayerId()) == 0))
1849                                   );
1850
1851    // Determine value of SH extension length.
1852    Int shExtnLengthInBit = 0;
1853    if (slice->getPPS()->getPocResetInfoPresentFlag())
1854    {
1855      shExtnLengthInBit += 2;
1856    }
1857    if (slice->getPocResetIdc() > 0)
1858    {
1859      shExtnLengthInBit += 6;
1860    }
1861    if (slice->getPocResetIdc() == 3)
1862    {
1863      shExtnLengthInBit += (slice->getSPS()->getBitsForPOC() + 1);
1864    }
1865
1866    if (!slice->getPocMsbValRequiredFlag() && slice->getVPS()->getVpsPocLsbAlignedFlag())
1867    {
1868      shExtnLengthInBit++;
1869    }
1870    else
1871    {
1872      if( slice->getPocMsbValRequiredFlag() )
1873      {
1874        slice->setPocMsbValPresentFlag( true );
1875      }
1876      else
1877      {
1878        slice->setPocMsbValPresentFlag( false );
1879      }
1880    }
1881
1882    if (slice->getPocMsbNeeded())
1883    {
1884      slice->setPocMsbValPresentFlag(true);
1885    }
1886
1887    if (slice->getPocMsbValPresentFlag())
1888    {
1889      UInt lengthVal = 1;
1890      UInt tempVal = (slice->getPocMsbVal() / maxPocLsb) + 1;
1891      assert ( tempVal );
1892      while( 1 != tempVal )
1893      {
1894        tempVal >>= 1;
1895        lengthVal += 2;
1896      }
1897      shExtnLengthInBit += lengthVal;
1898    }
1899    Int shExtnAdditionalBits = 0;
1900    if(shExtnLengthInBit % 8 != 0)
1901    {
1902      shExtnAdditionalBits = 8 - (shExtnLengthInBit % 8);
1903    }
1904    Int shExtnLength = (shExtnLengthInBit + shExtnAdditionalBits) / 8;
1905    WRITE_UVLC( shExtnLength, "slice_header_extension_length" );
1906
1907    if(slice->getPPS()->getPocResetInfoPresentFlag())
1908    {
1909      WRITE_CODE( slice->getPocResetIdc(), 2,                                 "poc_reset_idc");
1910    }
1911    if(slice->getPocResetIdc() > 0)
1912    {
1913      WRITE_CODE( slice->getPocResetPeriodId(), 6,                            "poc_reset_period_id");
1914    }
1915    if(slice->getPocResetIdc() == 3) 
1916    {
1917      WRITE_FLAG( slice->getFullPocResetFlag() ? 1 : 0,                       "full_poc_reset_flag");
1918      WRITE_CODE( slice->getPocLsbVal(), slice->getSPS()->getBitsForPOC(),  "poc_lsb_val");
1919    }
1920
1921    if( !slice->getPocMsbValRequiredFlag() && slice->getVPS()->getVpsPocLsbAlignedFlag() )
1922    {
1923      WRITE_FLAG( slice->getPocMsbValPresentFlag(),                           "poc_msb_cycle_val_present_flag" );
1924    }
1925
1926    if( slice->getPocMsbValPresentFlag() )
1927    {
1928      assert(slice->getPocMsbVal() % maxPocLsb == 0);
1929      WRITE_UVLC(slice->getPocMsbVal() / maxPocLsb, "poc_msb_cycle_val");
1930    }
1931
1932    for (Int i = 0; i < shExtnAdditionalBits; i++)
1933    {
1934      WRITE_FLAG( 1, "slice_segment_header_extension_data_bit");
1935    }
1936  }
1937
1938  shBitsWrittenTillNow += ( getNumberOfWrittenBits() - tmpBitsBeforeWriting );
1939 
1940  // Slice header byte_alignment() included in xAttachSliceDataToNalUnit
1941}
1942#endif
1943
1944Void TEncCavlc::codeVPSExtension (TComVPS *vps)
1945{
1946  Int NumOutputLayersInOutputLayerSet[MAX_VPS_LAYER_SETS_PLUS1];
1947  Int OlsHighestOutputLayerId[MAX_VPS_LAYER_SETS_PLUS1];
1948
1949  if( vps->getMaxLayers() > 1 && vps->getBaseLayerInternalFlag() )
1950  {
1951    codePTL( vps->getPTL(1), false, vps->getMaxTLayers() - 1 );
1952  }
1953
1954  UInt i = 0, j = 0;
1955
1956  WRITE_FLAG( vps->getSplittingFlag(),                 "splitting_flag" );
1957
1958  for(i = 0; i < MAX_VPS_NUM_SCALABILITY_TYPES; i++)
1959  {
1960    WRITE_FLAG( vps->getScalabilityMask(i),            "scalability_mask[i]" );
1961  }
1962
1963  for(j = 0; j < vps->getNumScalabilityTypes() - vps->getSplittingFlag(); j++)
1964  {
1965    WRITE_CODE( vps->getDimensionIdLen(j) - 1, 3,      "dimension_id_len_minus1[j]" );
1966  }
1967
1968  // The value of dimBitOffset[ NumScalabilityTypes ] is set equal to 6.
1969  if(vps->getSplittingFlag())
1970  {
1971    UInt splDimSum=0;
1972    for(j = 0; j < vps->getNumScalabilityTypes(); j++)
1973    {
1974      splDimSum+=(vps->getDimensionIdLen(j));
1975    }
1976    assert(splDimSum<=6);
1977  }
1978
1979  WRITE_FLAG( vps->getNuhLayerIdPresentFlag(),         "vps_nuh_layer_id_present_flag" );
1980  for(i = 1; i < vps->getMaxLayers(); i++)
1981  {
1982    if( vps->getNuhLayerIdPresentFlag() )
1983    {
1984      WRITE_CODE( vps->getLayerIdInNuh(i),     6,      "layer_id_in_nuh[i]" );
1985    }
1986
1987    if( !vps->getSplittingFlag() )
1988    {
1989      for(j = 0; j < vps->getNumScalabilityTypes(); j++)
1990      {
1991        UInt bits = vps->getDimensionIdLen(j);
1992        WRITE_CODE( vps->getDimensionId(i, j),   bits,   "dimension_id[i][j]" );
1993      }
1994    }
1995  }
1996
1997  WRITE_CODE( vps->getViewIdLen( ), 4, "view_id_len" );
1998  assert ( vps->getNumViews() >= (1<<vps->getViewIdLen()) );
1999
2000  if ( vps->getViewIdLen() > 0 )
2001  {
2002    for( i = 0; i < vps->getNumViews(); i++ )
2003    {
2004      WRITE_CODE( vps->getViewIdVal( i ), vps->getViewIdLen( ), "view_id_val[i]" );
2005    }
2006  }
2007
2008  for( Int layerCtr = 1; layerCtr < vps->getMaxLayers(); layerCtr++)
2009  {
2010    for( Int refLayerCtr = 0; refLayerCtr < layerCtr; refLayerCtr++)
2011    {
2012      WRITE_FLAG(vps->getDirectDependencyFlag(layerCtr, refLayerCtr), "direct_dependency_flag[i][j]" );
2013    }
2014  }
2015
2016  if( vps->getNumIndependentLayers() > 1 )
2017  {
2018    WRITE_UVLC( vps->getNumAddLayerSets(), "num_add_layer_sets" );
2019
2020    for( i = 0; i < vps->getNumAddLayerSets(); i++ )
2021    {
2022      for( j = 1; j < vps->getNumIndependentLayers(); j++ )
2023      {
2024        Int len = 1;
2025        while( (1 << len) < (vps->getNumLayersInTreePartition(j) + 1) )
2026        {
2027          len++;
2028        }
2029        WRITE_CODE(vps->getHighestLayerIdxPlus1(i, j), len, "highest_layer_idx_plus1[i][j]");
2030      }
2031    }
2032  }
2033
2034  WRITE_FLAG( vps->getMaxTSLayersPresentFlag(), "vps_sub_layers_max_minus1_present_flag");
2035  if( vps->getMaxTSLayersPresentFlag() )
2036  {
2037    for( i = 0; i < vps->getMaxLayers(); i++ )
2038    {
2039      WRITE_CODE(vps->getMaxTSLayersMinus1(i), 3, "sub_layers_vps_max_minus1[i]" );
2040    }
2041  }
2042
2043   WRITE_FLAG( vps->getMaxTidRefPresentFlag(), "max_tid_ref_present_flag");
2044   if (vps->getMaxTidRefPresentFlag())
2045   {
2046     for( i = 0; i < vps->getMaxLayers() - 1; i++)
2047     {
2048       for( j = i+1; j <= vps->getMaxLayers() - 1; j++)
2049       {
2050         if(vps->getDirectDependencyFlag(j, i))
2051         {
2052           WRITE_CODE(vps->getMaxTidIlRefPicsPlus1(i,j), 3, "max_tid_il_ref_pics_plus1[i][j]" );
2053         }
2054       }
2055     }
2056   }
2057   WRITE_FLAG( vps->getIlpSshSignalingEnabledFlag(), "all_ref_layers_active_flag" );
2058
2059  // Profile-tier-level signalling
2060  WRITE_UVLC( vps->getNumProfileTierLevel() - 1, "vps_num_profile_tier_level_minus1"); 
2061
2062  Int const numBitsForPtlIdx = vps->calculateLenOfSyntaxElement( vps->getNumProfileTierLevel() );
2063
2064  //Do something here to make sure the loop is correct to consider base layer internal stuff
2065
2066  for( Int idx = vps->getBaseLayerInternalFlag() ? 2 : 1; idx < vps->getNumProfileTierLevel(); idx++ )
2067  {
2068    vps->setProfilePresentFlag(idx, true);
2069
2070    WRITE_FLAG( vps->getProfilePresentFlag(idx),       "vps_profile_present_flag[i]" );
2071
2072    codePTL( vps->getPTL(idx), vps->getProfilePresentFlag(idx), vps->getMaxTLayers() - 1 );
2073  }
2074
2075  Int numOutputLayerSets = vps->getNumOutputLayerSets();
2076  Int numAddOutputLayerSets = numOutputLayerSets - (Int)vps->getNumLayerSets();
2077
2078  // The value of num_add_olss shall be in the range of 0 to 1023, inclusive.
2079  assert( numAddOutputLayerSets >= 0 && numAddOutputLayerSets < 1024 );
2080
2081  if( vps->getNumLayerSets() > 1 )
2082  {
2083    WRITE_UVLC( numAddOutputLayerSets, "num_add_olss" );
2084    WRITE_CODE( vps->getDefaultTargetOutputLayerIdc(), 2, "default_output_layer_idc" );
2085  }
2086
2087  for(i = 1; i < numOutputLayerSets; i++)
2088  {
2089    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx(i);
2090    if( vps->getNumLayerSets() > 2 && i >= vps->getNumLayerSets() )
2091    {
2092      Int numBits = 1;
2093      while ((1 << numBits) < (vps->getNumLayerSets() - 1))
2094      {
2095        numBits++;
2096      }
2097      WRITE_CODE( vps->getOutputLayerSetIdx(i) - 1, numBits, "layer_set_idx_for_ols_minus1"); 
2098    }
2099
2100    if( i > vps->getVpsNumLayerSetsMinus1() || vps->getDefaultTargetOutputLayerIdc() >= 2 ) //Instead of == 2, >= 2 is used to follow the agreement that value 3 should be interpreted as 2
2101    {
2102      for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
2103      {
2104        WRITE_FLAG( vps->getOutputLayerFlag(i,j), "output_layer_flag[i][j]");
2105      }
2106    }
2107
2108    for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
2109    {
2110      if( vps->getNecessaryLayerFlag(i, j) && (vps->getNumProfileTierLevel() - 1) > 0 )
2111      {
2112        WRITE_CODE( vps->getProfileLevelTierIdx(i, j), numBitsForPtlIdx, "profile_level_tier_idx[i]" );
2113      }
2114    }
2115
2116    NumOutputLayersInOutputLayerSet[i] = 0;
2117
2118    for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
2119    {
2120      NumOutputLayersInOutputLayerSet[i] += vps->getOutputLayerFlag(i, j);
2121      if( vps->getOutputLayerFlag(i, j) )
2122      {
2123        OlsHighestOutputLayerId[i] = vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, j);
2124      }
2125    }
2126    if( NumOutputLayersInOutputLayerSet[i] == 1 && vps->getNumDirectRefLayers(OlsHighestOutputLayerId[i]) > 0 )
2127    {
2128      WRITE_FLAG(vps->getAltOuputLayerFlag(i), "alt_output_layer_flag[i]");
2129    }
2130
2131    assert( NumOutputLayersInOutputLayerSet[i] > 0 );
2132  }
2133
2134  // The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
2135  assert( vps->getVpsNumRepFormats() > 0 && vps->getVpsNumRepFormats() <= 256 );
2136 
2137  WRITE_UVLC( vps->getVpsNumRepFormats() - 1, "vps_num_rep_formats_minus1" );
2138
2139  for(i = 0; i < vps->getVpsNumRepFormats(); i++)
2140  {
2141    // Write rep_format_structures
2142    codeRepFormat( vps->getVpsRepFormat(i) );
2143  }
2144
2145  if( vps->getVpsNumRepFormats() > 1 )
2146  {
2147    WRITE_FLAG( vps->getRepFormatIdxPresentFlag(), "rep_format_idx_present_flag"); 
2148  }
2149  else
2150  {
2151    // When not present, the value of rep_format_idx_present_flag is inferred to be equal to 0
2152    assert( !vps->getRepFormatIdxPresentFlag() );
2153  }
2154
2155  if( vps->getRepFormatIdxPresentFlag() )
2156  {
2157    for( i = vps->getBaseLayerInternalFlag() ? 1 : 0; i < vps->getMaxLayers(); i++ )
2158    {
2159      Int numBits = 1;
2160      while ((1 << numBits) < (vps->getVpsNumRepFormats()))
2161      {
2162        numBits++;
2163      }
2164      WRITE_CODE( vps->getVpsRepFormatIdx(i), numBits, "vps_rep_format_idx[i]" );
2165    }
2166  }
2167
2168  WRITE_FLAG(vps->getMaxOneActiveRefLayerFlag(), "max_one_active_ref_layer_flag");
2169
2170  WRITE_FLAG(vps->getVpsPocLsbAlignedFlag(), "vps_poc_lsb_aligned_flag");
2171
2172  for( i = 1; i< vps->getMaxLayers(); i++ )
2173  {
2174    if( vps->getNumDirectRefLayers( vps->getLayerIdInNuh(i) ) == 0  )
2175    {
2176      WRITE_FLAG(vps->getPocLsbNotPresentFlag(i), "poc_lsb_not_present_flag[i]");
2177    }
2178  }
2179 
2180#if VPS_DPB_SIZE_TABLE
2181  codeVpsDpbSizeTable(vps);
2182#endif
2183
2184  WRITE_UVLC( vps->getDirectDepTypeLen()-2,                           "direct_dep_type_len_minus2");
2185
2186  WRITE_FLAG(vps->getDefaultDirectDependencyTypeFlag(), "default_direct_dependency_flag");
2187
2188  if( vps->getDefaultDirectDependencyTypeFlag() )
2189  {
2190    WRITE_CODE( vps->getDefaultDirectDependencyType(), vps->getDirectDepTypeLen(), "default_direct_dependency_type" );
2191  }
2192  else
2193  {
2194    for( i = vps->getBaseLayerInternalFlag() ? 1 : 2; i < vps->getMaxLayers(); i++ )
2195    {
2196      for( j = vps->getBaseLayerInternalFlag() ? 0 : 1; j < i; j++ )
2197      {
2198        if (vps->getDirectDependencyFlag(i, j))
2199        {
2200          WRITE_CODE( vps->getDirectDependencyType(i, j), vps->getDirectDepTypeLen(), "direct_dependency_type[i][j]" );
2201        }
2202      }
2203    }
2204  }
2205
2206  // The value of vps_non_vui_extension_length shall be in the range of 0 to 4096, inclusive.
2207  assert( vps->getVpsNonVuiExtLength() >= 0 && vps->getVpsNonVuiExtLength() <= 4096 );
2208
2209  WRITE_UVLC( vps->getVpsNonVuiExtLength(), "vps_non_vui_extension_length" );
2210
2211  for( i = 1; i <= vps->getVpsNonVuiExtLength(); i++ )
2212  {
2213    WRITE_CODE(1, 8, "vps_non_vui_extension_data_byte");
2214  }
2215
2216  vps->setVpsVuiPresentFlag(true);
2217  WRITE_FLAG( vps->getVpsVuiPresentFlag() ? 1 : 0,                     "vps_vui_present_flag" );
2218
2219  if(vps->getVpsVuiPresentFlag())   // Should be conditioned on the value of vps_vui_present_flag
2220  {
2221    while ( m_pcBitIf->getNumberOfWrittenBits() % 8 != 0 )
2222    {
2223      WRITE_FLAG(1,                  "vps_vui_alignment_bit_equal_to_one");
2224    }
2225
2226    codeVPSVUI(vps); 
2227  }
2228}
2229
2230Void  TEncCavlc::codeRepFormat( RepFormat *repFormat )
2231{
2232  WRITE_CODE( repFormat->getPicWidthVpsInLumaSamples (), 16, "pic_width_vps_in_luma_samples" );   
2233  WRITE_CODE( repFormat->getPicHeightVpsInLumaSamples(), 16, "pic_height_vps_in_luma_samples" ); 
2234  WRITE_FLAG( repFormat->getChromaAndBitDepthVpsPresentFlag(), "chroma_and_bit_depth_vps_present_flag" );
2235
2236  if( repFormat->getChromaAndBitDepthVpsPresentFlag() )
2237  {
2238    WRITE_CODE( repFormat->getChromaFormatVpsIdc(), 2, "chroma_format_vps_idc" );   
2239
2240    if( repFormat->getChromaFormatVpsIdc() == 3 )
2241    {
2242      WRITE_FLAG( repFormat->getSeparateColourPlaneVpsFlag(), "separate_colour_plane_vps_flag" );     
2243    }
2244
2245    assert( repFormat->getBitDepthVpsLuma() >= 8 );
2246    assert( repFormat->getBitDepthVpsChroma() >= 8 );
2247    WRITE_CODE( repFormat->getBitDepthVpsLuma() - 8,   4, "bit_depth_vps_luma_minus8" );           
2248    WRITE_CODE( repFormat->getBitDepthVpsChroma() - 8, 4, "bit_depth_vps_chroma_minus8" );
2249  }
2250
2251  Window conf = repFormat->getConformanceWindowVps();
2252
2253  WRITE_FLAG( conf.getWindowEnabledFlag(),    "conformance_window_vps_flag" );
2254  if (conf.getWindowEnabledFlag())
2255  {
2256    WRITE_UVLC( conf.getWindowLeftOffset(),   "conf_win_vps_left_offset"   );
2257    WRITE_UVLC( conf.getWindowRightOffset(),  "conf_win_vps_right_offset"  );
2258    WRITE_UVLC( conf.getWindowTopOffset(),    "conf_win_vps_top_offset"    );
2259    WRITE_UVLC( conf.getWindowBottomOffset(), "conf_win_vps_bottom_offset" );
2260  }
2261}
2262
2263#if VPS_DPB_SIZE_TABLE
2264Void TEncCavlc::codeVpsDpbSizeTable(TComVPS *vps)
2265{
2266  for( Int i = 1; i < vps->getNumOutputLayerSets(); i++ )
2267  {
2268    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx( i );
2269
2270    WRITE_FLAG( vps->getSubLayerFlagInfoPresentFlag( i ), "sub_layer_flag_info_present_flag[i]");
2271
2272    for(Int j = 0; j <= vps->getMaxSLayersInLayerSetMinus1( layerSetIdxForOutputLayerSet ); j++)
2273    {
2274      if( j > 0 && vps->getSubLayerFlagInfoPresentFlag(i) )
2275      {
2276        WRITE_FLAG( vps->getSubLayerDpbInfoPresentFlag( i, j), "sub_layer_dpb_info_present_flag[i]"); 
2277      }
2278
2279      if( vps->getSubLayerDpbInfoPresentFlag(i, j) )
2280      {
2281        for(Int k = 0; k < vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet ); k++)
2282        {
2283          if( vps->getNecessaryLayerFlag(i, k) && (vps->getBaseLayerInternalFlag() || (vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, k) != 0)) )
2284          {
2285            WRITE_UVLC( vps->getMaxVpsDecPicBufferingMinus1( i, k, j ), "max_vps_dec_pic_buffering_minus1[i][k][j]" );
2286          }
2287        }
2288
2289        WRITE_UVLC( vps->getMaxVpsNumReorderPics( i, j), "max_vps_num_reorder_pics[i][j]" );             
2290
2291        WRITE_UVLC( vps->getMaxVpsLatencyIncreasePlus1( i, j), "max_vps_latency_increase_plus1[i][j]" );       
2292      }
2293    }
2294  }
2295}
2296#endif
2297
2298Void TEncCavlc::codeVPSVUI (TComVPS *vps)
2299{
2300  Int i,j;
2301  WRITE_FLAG(vps->getCrossLayerPictureTypeAlignFlag(), "cross_layer_pic_type_aligned_flag");
2302  if (!vps->getCrossLayerPictureTypeAlignFlag())
2303  {
2304    WRITE_FLAG(vps->getCrossLayerIrapAlignFlag(), "cross_layer_irap_aligned_flag");
2305  }
2306  else
2307  {
2308    // When not present, the value of cross_layer_irap_aligned_flag is inferred to be equal to vps_vui_present_flag,
2309    // i.e. it is true in this function
2310    vps->setCrossLayerIrapAlignFlag( true ); 
2311  }
2312
2313  if( vps->getCrossLayerIrapAlignFlag() )
2314  {
2315    WRITE_FLAG(vps->getCrossLayerAlignedIdrOnlyFlag(), "all_layers_idr_aligned_flag");
2316  }
2317
2318  WRITE_FLAG( vps->getBitRatePresentVpsFlag(),        "bit_rate_present_vps_flag" );
2319  WRITE_FLAG( vps->getPicRatePresentVpsFlag(),        "pic_rate_present_vps_flag" );
2320
2321  if( vps->getBitRatePresentVpsFlag() || vps->getPicRatePresentVpsFlag() )
2322  {
2323    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getNumLayerSets(); i++ )
2324    {
2325      for( j = 0; j <= vps->getMaxSLayersInLayerSetMinus1(i); j++ )
2326      {
2327        if( vps->getBitRatePresentVpsFlag() )
2328        {
2329          WRITE_FLAG( vps->getBitRatePresentFlag( i, j),        "bit_rate_present_flag[i][j]" );
2330        }
2331
2332        if( vps->getPicRatePresentVpsFlag() )
2333        {
2334          WRITE_FLAG( vps->getPicRatePresentFlag( i, j),        "pic_rate_present_flag[i][j]" );
2335        }
2336
2337        if( vps->getBitRatePresentFlag(i, j) )
2338        {
2339          WRITE_CODE( vps->getAvgBitRate( i, j ), 16, "avg_bit_rate[i][j]" );
2340          WRITE_CODE( vps->getAvgBitRate( i, j ), 16, "max_bit_rate[i][j]" );
2341        }
2342
2343        if( vps->getPicRatePresentFlag(i, j) )
2344        {
2345          WRITE_CODE( vps->getConstPicRateIdc( i, j), 2 , "constant_pic_rate_idc[i][j]" ); 
2346          WRITE_CODE( vps->getConstPicRateIdc( i, j), 16, "avg_pic_rate[i][j]"          ); 
2347        }
2348      }
2349    }
2350  }
2351
2352  WRITE_FLAG( vps->getVideoSigPresentVpsFlag(), "video_signal_info_idx_present_flag" );
2353  if (vps->getVideoSigPresentVpsFlag())
2354  {
2355    WRITE_CODE(vps->getNumVideoSignalInfo()-1, 4, "vps_num_video_signal_info_minus1" );
2356  }
2357
2358  for(i = 0; i < vps->getNumVideoSignalInfo(); i++)
2359  {
2360    WRITE_CODE(vps->getVideoVPSFormat(i), 3, "video_vps_format" );
2361    WRITE_FLAG(vps->getVideoFullRangeVpsFlag(i), "video_full_range_vps_flag" );
2362    WRITE_CODE(vps->getColorPrimaries(i), 8, "color_primaries_vps" );
2363    WRITE_CODE(vps->getTransCharacter(i), 8, "transfer_characteristics_vps" );
2364    WRITE_CODE(vps->getMaxtrixCoeff(i), 8, "matrix_coeffs_vps" );
2365  }
2366
2367  if( vps->getVideoSigPresentVpsFlag() && vps->getNumVideoSignalInfo() > 1 )
2368  {
2369    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++ )
2370    {
2371      WRITE_CODE( vps->getVideoSignalInfoIdx(i), 4, "vps_video_signal_info_idx" );
2372    }
2373  }
2374
2375  WRITE_FLAG( vps->getTilesNotInUseFlag() ? 1 : 0 , "tiles_not_in_use_flag" );
2376
2377  if( !vps->getTilesNotInUseFlag() )
2378  {
2379    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++ )
2380    {
2381      WRITE_FLAG( vps->getTilesInUseFlag(i) ? 1 : 0 , "tiles_in_use_flag[ i ]" );
2382
2383      if( vps->getTilesInUseFlag(i) )
2384      {
2385        WRITE_FLAG( vps->getLoopFilterNotAcrossTilesFlag(i) ? 1 : 0 , "loop_filter_not_across_tiles_flag[ i ]" );
2386      }
2387    }
2388
2389    for( i = vps->getBaseLayerInternalFlag() ? 1 : 2; i < vps->getMaxLayers(); i++ )
2390    {
2391      for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
2392      {
2393        UInt layerIdx = vps->getLayerIdxInVps(vps->getRefLayerId(vps->getLayerIdInNuh(i), j));
2394
2395        if( vps->getTilesInUseFlag(i) && vps->getTilesInUseFlag(layerIdx) )
2396        {
2397          WRITE_FLAG( vps->getTileBoundariesAlignedFlag(i,j) ? 1 : 0 , "tile_boundaries_aligned_flag[i][j]" );
2398        }
2399      }
2400    } 
2401  }
2402
2403  WRITE_FLAG( vps->getWppNotInUseFlag() ? 1 : 0 , "wpp_not_in_use_flag" );
2404
2405  if( !vps->getWppNotInUseFlag() )
2406  {
2407    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++ )
2408    {
2409      WRITE_FLAG( vps->getWppInUseFlag(i) ? 1 : 0 , "wpp_in_use_flag[ i ]" );
2410    }
2411  }
2412
2413  WRITE_FLAG(vps->getSingleLayerForNonIrapFlag(), "single_layer_for_non_irap_flag" );
2414
2415  // When single_layer_for_non_irap_flag is equal to 0, higher_layer_irap_skip_flag shall be equal to 0
2416  if( !vps->getSingleLayerForNonIrapFlag() )
2417  {
2418    assert( !vps->getHigherLayerIrapSkipFlag() );
2419  }
2420
2421  WRITE_FLAG(vps->getHigherLayerIrapSkipFlag(), "higher_layer_irap_skip_flag" );
2422
2423  WRITE_FLAG( vps->getIlpRestrictedRefLayersFlag() ? 1 : 0 , "ilp_restricted_ref_layers_flag" );   
2424  if( vps->getIlpRestrictedRefLayersFlag())
2425  {
2426    for(i = 1; i < vps->getMaxLayers(); i++)
2427    {
2428      for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
2429      {
2430        if (vps->getBaseLayerInternalFlag() || vps->getRefLayerId(vps->getLayerIdInNuh(i), j))
2431        {
2432          WRITE_UVLC(vps->getMinSpatialSegmentOffsetPlus1( i, j),    "min_spatial_segment_offset_plus1[i][j]");
2433
2434          if( vps->getMinSpatialSegmentOffsetPlus1(i,j ) > 0 ) 
2435          { 
2436            WRITE_FLAG( vps->getCtuBasedOffsetEnabledFlag( i, j) ? 1 : 0 , "ctu_based_offset_enabled_flag[i][j]" );   
2437
2438            if(vps->getCtuBasedOffsetEnabledFlag(i,j)) 
2439            {
2440              WRITE_UVLC(vps->getMinHorizontalCtuOffsetPlus1( i, j),    "min_horizontal_ctu_offset_plus1[i][j]");           
2441            }
2442          }
2443        }
2444      } 
2445    }
2446  }
2447
2448#if O0164_MULTI_LAYER_HRD
2449  WRITE_FLAG(vps->getVpsVuiBspHrdPresentFlag(), "vps_vui_bsp_hrd_present_flag" );
2450
2451  if( vps->getVpsVuiBspHrdPresentFlag() )
2452  {
2453    codeVpsVuiBspHrdParams(vps);
2454  }
2455#endif
2456
2457  for( i = 1; i < vps->getMaxLayers(); i++ )
2458  {
2459    if( vps->getNumRefLayers(vps->getLayerIdInNuh(i)) == 0 ) 
2460    {
2461      WRITE_FLAG(vps->getBaseLayerPSCompatibilityFlag(i), "base_layer_parameter_set_compatibility_flag" );
2462    }
2463  }
2464}
2465
2466Void TEncCavlc::codeSPSExtension( TComSPS* pcSPS )
2467{
2468  // more syntax elements to be written here
2469
2470  // Vertical MV component restriction is not used in SHVC CTC
2471  WRITE_FLAG( 0, "inter_view_mv_vert_constraint_flag" );
2472}
2473
2474#if Q0048_CGS_3D_ASYMLUT
2475Void TEncCavlc::xCode3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT )
2476{
2477#if R0150_CGS_SIGNAL_CONSTRAINTS
2478  UInt uiNumRefLayers = ( UInt )pc3DAsymLUT->getRefLayerNum();
2479  WRITE_UVLC( uiNumRefLayers - 1 , "num_cm_ref_layers_minus1" );
2480  for( UInt i = 0 ; i < uiNumRefLayers ; i++ )
2481  {
2482    WRITE_CODE( pc3DAsymLUT->getRefLayerId( i ) , 6 , "cm_ref_layer_id" );
2483  }
2484#endif
2485  assert( pc3DAsymLUT->getCurOctantDepth() < 4 );
2486  WRITE_CODE( pc3DAsymLUT->getCurOctantDepth() , 2 , "cm_octant_depth" );
2487  assert( pc3DAsymLUT->getCurYPartNumLog2() < 4 );
2488  WRITE_CODE( pc3DAsymLUT->getCurYPartNumLog2() , 2 , "cm_y_part_num_log2" );
2489  assert( pc3DAsymLUT->getInputBitDepthY() < 16 );
2490#if R0150_CGS_SIGNAL_CONSTRAINTS
2491  WRITE_UVLC( pc3DAsymLUT->getInputBitDepthY() - 8 , "cm_input_luma_bit_depth_minus8" );
2492  WRITE_UVLC( pc3DAsymLUT->getInputBitDepthC() - 8 , "cm_input_chroma_bit_depth_minus8" );
2493  WRITE_UVLC( pc3DAsymLUT->getOutputBitDepthY() - 8 , "cm_output_luma_bit_depth_minus8" );
2494  WRITE_UVLC( pc3DAsymLUT->getOutputBitDepthC() - 8 , "cm_output_chroma_bit_depth_minus8" );
2495#else
2496  WRITE_CODE( pc3DAsymLUT->getInputBitDepthY() - 8 , 3 , "cm_input_bit_depth_minus8" );
2497  WRITE_SVLC(pc3DAsymLUT->getInputBitDepthC()-pc3DAsymLUT->getInputBitDepthY(), "cm_input_bit_depth_chroma delta");
2498  assert( pc3DAsymLUT->getOutputBitDepthY() < 16 );
2499  WRITE_CODE( pc3DAsymLUT->getOutputBitDepthY() - 8 , 3 , "cm_output_bit_depth_minus8" );
2500  WRITE_SVLC(pc3DAsymLUT->getOutputBitDepthC()-pc3DAsymLUT->getOutputBitDepthY(), "cm_output_bit_depth_chroma_delta");
2501#endif
2502  assert( pc3DAsymLUT->getResQuantBit() < 4 );
2503  WRITE_CODE( pc3DAsymLUT->getResQuantBit() , 2 , "cm_res_quant_bit" );
2504#if R0300_CGS_RES_COEFF_CODING
2505  xFindDeltaBits( pc3DAsymLUT );
2506  assert(pc3DAsymLUT->getDeltaBits() >=1 && pc3DAsymLUT->getDeltaBits() <= 4);
2507  WRITE_CODE( pc3DAsymLUT->getDeltaBits()-1 , 2 , "cm_delta_bit" );
2508#endif
2509#if R0151_CGS_3D_ASYMLUT_IMPROVE
2510  if( pc3DAsymLUT->getCurOctantDepth() == 1 )
2511  {
2512    WRITE_SVLC( pc3DAsymLUT->getAdaptChromaThresholdU() - ( 1 << ( pc3DAsymLUT->getInputBitDepthC() - 1 ) ) , "cm_adapt_threshold_u_delta" );
2513    WRITE_SVLC( pc3DAsymLUT->getAdaptChromaThresholdV() - ( 1 << ( pc3DAsymLUT->getInputBitDepthC() - 1 ) ) , "cm_adapt_threshold_v_delta" );
2514  }
2515#endif
2516
2517#if R0164_CGS_LUT_BUGFIX_CHECK
2518  pc3DAsymLUT->xInitCuboids();
2519#endif
2520  xCode3DAsymLUTOctant( pc3DAsymLUT , 0 , 0 , 0 , 0 , 1 << pc3DAsymLUT->getCurOctantDepth() );
2521#if R0164_CGS_LUT_BUGFIX_CHECK
2522  xCuboidsFilledCheck( false );
2523  pc3DAsymLUT->display( false );
2524#endif
2525}
2526
2527Void TEncCavlc::xCode3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength )
2528{
2529  UInt uiOctantSplit = nDepth < pc3DAsymLUT->getCurOctantDepth();
2530  if( nDepth < pc3DAsymLUT->getCurOctantDepth() )
2531    WRITE_FLAG( uiOctantSplit , "split_octant_flag" );
2532  Int nYPartNum = 1 << pc3DAsymLUT->getCurYPartNumLog2();
2533  if( uiOctantSplit )
2534  {
2535    Int nHalfLength = nLength >> 1;
2536    for( Int l = 0 ; l < 2 ; l++ )
2537    {
2538      for( Int m = 0 ; m < 2 ; m++ )
2539      {
2540        for( Int n = 0 ; n < 2 ; n++ )
2541        {
2542          xCode3DAsymLUTOctant( pc3DAsymLUT , nDepth + 1 , yIdx + l * nHalfLength * nYPartNum , uIdx + m * nHalfLength , vIdx + n * nHalfLength , nHalfLength );
2543        }
2544      }
2545    }
2546  }
2547  else
2548  {
2549#if R0300_CGS_RES_COEFF_CODING
2550    Int nFLCbits = pc3DAsymLUT->getMappingShift()-pc3DAsymLUT->getResQuantBit()-pc3DAsymLUT->getDeltaBits() ; 
2551    nFLCbits = nFLCbits >= 0 ? nFLCbits : 0;
2552#endif
2553    for( Int l = 0 ; l < nYPartNum ; l++ )
2554    {
2555#if R0164_CGS_LUT_BUGFIX     
2556      Int shift = pc3DAsymLUT->getCurOctantDepth() - nDepth ;
2557#endif
2558      for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
2559      {
2560#if R0164_CGS_LUT_BUGFIX
2561        SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + (l<<shift) , uIdx , vIdx , nVertexIdx );
2562#else
2563        SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + l , uIdx , vIdx , nVertexIdx );
2564#endif
2565        UInt uiCodeVertex = sRes.Y != 0 || sRes.U != 0 || sRes.V != 0;
2566        WRITE_FLAG( uiCodeVertex , "coded_vertex_flag" );
2567        if( uiCodeVertex )
2568        {
2569#if R0151_CGS_3D_ASYMLUT_IMPROVE
2570#if R0300_CGS_RES_COEFF_CODING
2571          xWriteParam( sRes.Y, nFLCbits );
2572          xWriteParam( sRes.U, nFLCbits );
2573          xWriteParam( sRes.V, nFLCbits );
2574#else
2575          xWriteParam( sRes.Y );
2576          xWriteParam( sRes.U );
2577          xWriteParam( sRes.V );
2578#endif
2579#else
2580          WRITE_SVLC( sRes.Y , "resY" );
2581          WRITE_SVLC( sRes.U , "resU" );
2582          WRITE_SVLC( sRes.V , "resV" );
2583#endif
2584        }
2585      }
2586#if R0164_CGS_LUT_BUGFIX_CHECK
2587      pc3DAsymLUT->xSetExplicit( yIdx + (l<<shift) , uIdx , vIdx );
2588#endif
2589    }
2590  }
2591}
2592
2593#if R0151_CGS_3D_ASYMLUT_IMPROVE
2594#if R0300_CGS_RES_COEFF_CODING
2595Void TEncCavlc::xWriteParam( Int param, UInt rParam)
2596#else
2597Void TEncCavlc::xWriteParam( Int param)
2598#endif
2599{
2600#if !R0300_CGS_RES_COEFF_CODING
2601  const UInt rParam = 7;
2602#endif
2603  Int codeNumber = abs(param);
2604  WRITE_UVLC(codeNumber / (1 << rParam), "quotient");
2605  WRITE_CODE((codeNumber % (1 << rParam)), rParam, "remainder");
2606  if (abs(param))
2607    WRITE_FLAG( param <0, "sign");
2608}
2609#endif
2610
2611#if R0300_CGS_RES_COEFF_CODING
2612Void TEncCavlc::xFindDeltaBits( TCom3DAsymLUT * pc3DAsymLUT )
2613{
2614  Int nDeltaBits; 
2615  Int nBestDeltaBits = -1; 
2616  Int nBestBits = MAX_INT; 
2617  for( nDeltaBits = 1; nDeltaBits < 5; nDeltaBits++)
2618  {
2619    Int nCurBits = 0;
2620    xTally3DAsymLUTOctantBits( pc3DAsymLUT , 0 , 0 , 0 , 0 , 1 << pc3DAsymLUT->getCurOctantDepth(), nDeltaBits, nCurBits );
2621    //printf("%d, %d, %d\n", nDeltaBits, nCurBits, nBestBits);
2622    if(nCurBits < nBestBits)
2623    {
2624      nBestDeltaBits = nDeltaBits; 
2625      nBestBits = nCurBits;
2626    }
2627  }
2628
2629  assert(nBestDeltaBits >=1 && nBestDeltaBits < 5);
2630  pc3DAsymLUT->setDeltaBits(nBestDeltaBits); 
2631}
2632
2633Void TEncCavlc::xTally3DAsymLUTOctantBits( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength, Int nDeltaBits, Int& nCurBits )
2634{
2635  UInt uiOctantSplit = nDepth < pc3DAsymLUT->getCurOctantDepth();
2636  if( nDepth < pc3DAsymLUT->getCurOctantDepth() )
2637    nCurBits ++; 
2638  Int nYPartNum = 1 << pc3DAsymLUT->getCurYPartNumLog2();
2639  if( uiOctantSplit )
2640  {
2641    Int nHalfLength = nLength >> 1;
2642    for( Int l = 0 ; l < 2 ; l++ )
2643    {
2644      for( Int m = 0 ; m < 2 ; m++ )
2645      {
2646        for( Int n = 0 ; n < 2 ; n++ )
2647        {
2648          xTally3DAsymLUTOctantBits( pc3DAsymLUT , nDepth + 1 , yIdx + l * nHalfLength * nYPartNum , uIdx + m * nHalfLength , vIdx + n * nHalfLength , nHalfLength, nDeltaBits, nCurBits );
2649        }
2650      }
2651    }
2652  }
2653  else
2654  {
2655    Int nFLCbits = pc3DAsymLUT->getMappingShift()-pc3DAsymLUT->getResQuantBit()-nDeltaBits ; 
2656    nFLCbits = nFLCbits >= 0 ? nFLCbits:0;
2657    //printf("nFLCbits = %d\n", nFLCbits);
2658
2659    for( Int l = 0 ; l < nYPartNum ; l++ )
2660    {
2661      for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
2662      {
2663        SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + l , uIdx , vIdx , nVertexIdx );
2664
2665        UInt uiCodeVertex = sRes.Y != 0 || sRes.U != 0 || sRes.V != 0;
2666        nCurBits++;
2667        if( uiCodeVertex )
2668        {
2669          xCheckParamBits( sRes.Y, nFLCbits, nCurBits );
2670          xCheckParamBits( sRes.U, nFLCbits, nCurBits );
2671          xCheckParamBits( sRes.V, nFLCbits, nCurBits );
2672        }
2673      }
2674    }
2675  }
2676}
2677
2678Void TEncCavlc::xCheckParamBits( Int param, Int rParam, Int &nBits)
2679{
2680  Int codeNumber = abs(param);
2681  Int codeQuotient = codeNumber >> rParam;
2682  Int qLen; 
2683
2684  UInt uiLength = 1;
2685  UInt uiTemp = ++codeQuotient;
2686   
2687  while( 1 != uiTemp )
2688  {
2689    uiTemp >>= 1;
2690    uiLength += 2;
2691  }
2692
2693  qLen  = (uiLength >> 1);
2694  qLen += ((uiLength+1) >> 1);
2695
2696  nBits += qLen; 
2697  nBits += rParam; 
2698  if (abs(param))
2699    nBits++; 
2700}
2701#endif
2702#endif
2703
2704Void TEncCavlc::codeVpsVuiBspHrdParams(TComVPS * const vps)
2705{
2706  WRITE_UVLC( vps->getVpsNumAddHrdParams(), "vps_num_add_hrd_params" );
2707
2708  for( Int i = vps->getNumHrdParameters(), j = 0; i < vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams(); i++, j++ ) // j = i - vps->getNumHrdParameters()
2709  {
2710    if( i > 0 )
2711    {
2712      WRITE_FLAG( vps->getCprmsAddPresentFlag(j), "cprms_add_present_flag[i]" );
2713    }
2714
2715    WRITE_UVLC( vps->getNumSubLayerHrdMinus1(j), "num_sub_layer_hrd_minus1[i]" );
2716
2717    codeHrdParameters(vps->getBspHrd(j), i == 0 ? true : vps->getCprmsAddPresentFlag(j), vps->getNumSubLayerHrdMinus1(j));
2718  }
2719
2720  if( vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams() > 0 )
2721  {
2722    for( Int h = 1; h < vps->getNumOutputLayerSets(); h++ )
2723    {
2724      Int lsIdx = vps->getOutputLayerSetIdx( h );
2725
2726      WRITE_UVLC( vps->getNumSignalledPartitioningSchemes(h), "num_signalled_partitioning_schemes[h]");
2727
2728      for( Int j = 1; j < vps->getNumSignalledPartitioningSchemes(h) + 1; j++ )
2729      {
2730        WRITE_UVLC( vps->getNumPartitionsInSchemeMinus1(h, j), "num_partitions_in_scheme_minus1[h][j]" );
2731
2732        for( Int k = 0; k <= vps->getNumPartitionsInSchemeMinus1(h, j); k++ )
2733        {
2734          for( Int r = 0; r < vps->getNumLayersInIdList( lsIdx ); r++ )
2735          {
2736            WRITE_FLAG( vps->getLayerIncludedInPartitionFlag(h, j, k, r), "layer_included_in_partition_flag[h][j][k][r]" );
2737          }
2738        }
2739      }
2740
2741      for( Int i = 0; i < vps->getNumSignalledPartitioningSchemes(h) + 1; i++ )
2742      {
2743        for( Int t = 0; t <= vps->getMaxSLayersInLayerSetMinus1(lsIdx); t++ )
2744        {
2745          WRITE_UVLC(vps->getNumBspSchedulesMinus1(h, i, t), "num_bsp_schedules_minus1[h][i][t]");
2746
2747          for( Int j = 0; j <= vps->getNumBspSchedulesMinus1(h, i, t); j++ )
2748          {
2749            for( Int k = 0; k <= vps->getNumPartitionsInSchemeMinus1(h, i); k++ )
2750            {
2751              if( vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams() > 1 )
2752              {
2753                Int numBits = 1;
2754                while ((1 << numBits) < (vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams()))
2755                {
2756                  numBits++;
2757                }
2758
2759                WRITE_CODE(vps->getBspHrdIdx(h, i, t, j, k), numBits, "bsp_comb_hrd_idx[h][i][t][j][k]");
2760              }
2761
2762              WRITE_UVLC( vps->getBspSchedIdx(h, i, t, j, k), "bsp_comb_sched_idx[h][i][t][j][k]");
2763            }
2764          }
2765        }
2766      }
2767    }
2768  }
2769}
2770
2771#endif //SVC_EXTENSION
2772//! \}
Note: See TracBrowser for help on using the repository browser.