source: 3DVCSoftware/branches/HTM-14.1-update-dev1-RWTH/source/Lib/TLibEncoder/TEncCavlc.cpp @ 1243

Last change on this file since 1243 was 1243, checked in by rwth, 9 years ago
  • fixed problem when NH_3D_DMM=0
  • cleaned TEMP_SDC_CLEANUP
  • Property svn:eol-style set to native
File size: 110.0 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2015, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncCavlc.cpp
35    \brief    CAVLC encoder class
36*/
37
38#include "../TLibCommon/CommonDef.h"
39#include "TEncCavlc.h"
40#include "SEIwrite.h"
41
42#if NH_3D
43#include "TEncTop.h"
44#endif
45
46//! \ingroup TLibEncoder
47//! \{
48
49#if ENC_DEC_TRACE
50
51#if !H_MV_ENC_DEC_TRAC
52Void  xTraceVPSHeader ()
53{
54  fprintf( g_hTrace, "=========== Video Parameter Set     ===========\n" );
55
56Void  xTraceSPSHeader ()
57{
58  fprintf( g_hTrace, "=========== Sequence Parameter Set  ===========\n" );
59}
60
61Void  xTracePPSHeader ()
62{
63  fprintf( g_hTrace, "=========== Picture Parameter Set  ===========\n");
64}
65
66Void  xTraceSliceHeader ( )
67{
68  fprintf( g_hTrace, "=========== Slice ===========\n");
69}
70#endif
71#endif
72// ====================================================================================================================
73// Constructor / destructor / create / destroy
74// ====================================================================================================================
75
76TEncCavlc::TEncCavlc()
77{
78  m_pcBitIf           = NULL;
79}
80
81TEncCavlc::~TEncCavlc()
82{
83}
84
85
86// ====================================================================================================================
87// Public member functions
88// ====================================================================================================================
89
90Void TEncCavlc::resetEntropy(const TComSlice* /*pSlice*/)
91{
92}
93
94
95Void TEncCavlc::codeShortTermRefPicSet( const TComReferencePictureSet* rps, Bool calledFromSliceHeader, Int idx)
96{
97#if PRINT_RPS_INFO
98  Int lastBits = getNumberOfWrittenBits();
99#endif
100  if (idx > 0)
101  {
102  WRITE_FLAG( rps->getInterRPSPrediction(), "inter_ref_pic_set_prediction_flag" ); // inter_RPS_prediction_flag
103  }
104  if (rps->getInterRPSPrediction())
105  {
106    Int deltaRPS = rps->getDeltaRPS();
107    if(calledFromSliceHeader)
108    {
109      WRITE_UVLC( rps->getDeltaRIdxMinus1(), "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
110    }
111
112    WRITE_CODE( (deltaRPS >=0 ? 0: 1), 1, "delta_rps_sign" ); //delta_rps_sign
113    WRITE_UVLC( abs(deltaRPS) - 1, "abs_delta_rps_minus1"); // absolute delta RPS minus 1
114
115    for(Int j=0; j < rps->getNumRefIdc(); j++)
116    {
117      Int refIdc = rps->getRefIdc(j);
118      WRITE_CODE( (refIdc==1? 1: 0), 1, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
119      if (refIdc != 1)
120      {
121        WRITE_CODE( refIdc>>1, 1, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
122      }
123    }
124  }
125  else
126  {
127    WRITE_UVLC( rps->getNumberOfNegativePictures(), "num_negative_pics" );
128    WRITE_UVLC( rps->getNumberOfPositivePictures(), "num_positive_pics" );
129    Int prev = 0;
130    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
131    {
132      WRITE_UVLC( prev-rps->getDeltaPOC(j)-1, "delta_poc_s0_minus1" );
133      prev = rps->getDeltaPOC(j);
134      WRITE_FLAG( rps->getUsed(j), "used_by_curr_pic_s0_flag");
135    }
136    prev = 0;
137    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
138    {
139      WRITE_UVLC( rps->getDeltaPOC(j)-prev-1, "delta_poc_s1_minus1" );
140      prev = rps->getDeltaPOC(j);
141      WRITE_FLAG( rps->getUsed(j), "used_by_curr_pic_s1_flag" );
142    }
143  }
144
145#if PRINT_RPS_INFO
146  printf("irps=%d (%2d bits) ", rps->getInterRPSPrediction(), getNumberOfWrittenBits() - lastBits);
147  rps->printDeltaPOC();
148#endif
149}
150
151
152Void TEncCavlc::codePPS( const TComPPS* pcPPS )
153{
154#if ENC_DEC_TRACE
155#if H_MV_ENC_DEC_TRAC
156  tracePSHeader( "PPS", pcPPS->getLayerId() ); 
157#else
158  xTracePPSHeader ();
159#endif
160#endif
161  WRITE_UVLC( pcPPS->getPPSId(),                             "pps_pic_parameter_set_id" );
162  WRITE_UVLC( pcPPS->getSPSId(),                             "pps_seq_parameter_set_id" );
163  WRITE_FLAG( pcPPS->getDependentSliceSegmentsEnabledFlag()    ? 1 : 0, "dependent_slice_segments_enabled_flag" );
164  WRITE_FLAG( pcPPS->getOutputFlagPresentFlag() ? 1 : 0,     "output_flag_present_flag" );
165  WRITE_CODE( pcPPS->getNumExtraSliceHeaderBits(), 3,        "num_extra_slice_header_bits");
166  WRITE_FLAG( pcPPS->getSignHideFlag(), "sign_data_hiding_flag" );
167  WRITE_FLAG( pcPPS->getCabacInitPresentFlag() ? 1 : 0,   "cabac_init_present_flag" );
168#if PPS_FIX_DEPTH
169  if( pcPPS->getSPS()->getVPS()->getDepthId(pcPPS->getSPS()->getLayerId()) )
170  {
171    WRITE_UVLC( pcPPS->getNumRefIdxL0DefaultActive(),     "num_ref_idx_l0_default_active_minus1");
172    WRITE_UVLC( pcPPS->getNumRefIdxL1DefaultActive(),     "num_ref_idx_l1_default_active_minus1");
173  }
174  else
175  {
176#endif
177  WRITE_UVLC( pcPPS->getNumRefIdxL0DefaultActive()-1,     "num_ref_idx_l0_default_active_minus1");
178  WRITE_UVLC( pcPPS->getNumRefIdxL1DefaultActive()-1,     "num_ref_idx_l1_default_active_minus1");
179#if PPS_FIX_DEPTH
180  }
181#endif
182  WRITE_SVLC( pcPPS->getPicInitQPMinus26(),                  "init_qp_minus26");
183  WRITE_FLAG( pcPPS->getConstrainedIntraPred() ? 1 : 0,      "constrained_intra_pred_flag" );
184  WRITE_FLAG( pcPPS->getUseTransformSkip() ? 1 : 0,  "transform_skip_enabled_flag" );
185  WRITE_FLAG( pcPPS->getUseDQP() ? 1 : 0, "cu_qp_delta_enabled_flag" );
186  if ( pcPPS->getUseDQP() )
187  {
188    WRITE_UVLC( pcPPS->getMaxCuDQPDepth(), "diff_cu_qp_delta_depth" );
189  }
190
191  WRITE_SVLC( pcPPS->getQpOffset(COMPONENT_Cb), "pps_cb_qp_offset" );
192  WRITE_SVLC( pcPPS->getQpOffset(COMPONENT_Cr), "pps_cr_qp_offset" );
193
194  WRITE_FLAG( pcPPS->getSliceChromaQpFlag() ? 1 : 0,          "pps_slice_chroma_qp_offsets_present_flag" );
195
196  WRITE_FLAG( pcPPS->getUseWP() ? 1 : 0,  "weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
197  WRITE_FLAG( pcPPS->getWPBiPred() ? 1 : 0, "weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
198  WRITE_FLAG( pcPPS->getTransquantBypassEnableFlag() ? 1 : 0, "transquant_bypass_enable_flag" );
199  WRITE_FLAG( pcPPS->getTilesEnabledFlag()             ? 1 : 0, "tiles_enabled_flag" );
200  WRITE_FLAG( pcPPS->getEntropyCodingSyncEnabledFlag() ? 1 : 0, "entropy_coding_sync_enabled_flag" );
201  if( pcPPS->getTilesEnabledFlag() )
202  {
203    WRITE_UVLC( pcPPS->getNumTileColumnsMinus1(),                                    "num_tile_columns_minus1" );
204    WRITE_UVLC( pcPPS->getNumTileRowsMinus1(),                                       "num_tile_rows_minus1" );
205    WRITE_FLAG( pcPPS->getTileUniformSpacingFlag(),                                  "uniform_spacing_flag" );
206    if( !pcPPS->getTileUniformSpacingFlag() )
207    {
208      for(UInt i=0; i<pcPPS->getNumTileColumnsMinus1(); i++)
209      {
210        WRITE_UVLC( pcPPS->getTileColumnWidth(i)-1,                                  "column_width_minus1" );
211      }
212      for(UInt i=0; i<pcPPS->getNumTileRowsMinus1(); i++)
213      {
214        WRITE_UVLC( pcPPS->getTileRowHeight(i)-1,                                    "row_height_minus1" );
215      }
216    }
217    if(pcPPS->getNumTileColumnsMinus1() !=0 || pcPPS->getNumTileRowsMinus1() !=0)
218    {
219      WRITE_FLAG( pcPPS->getLoopFilterAcrossTilesEnabledFlag()?1 : 0,          "loop_filter_across_tiles_enabled_flag");
220    }
221  }
222  WRITE_FLAG( pcPPS->getLoopFilterAcrossSlicesEnabledFlag()?1 : 0,        "pps_loop_filter_across_slices_enabled_flag");
223  WRITE_FLAG( pcPPS->getDeblockingFilterControlPresentFlag()?1 : 0,       "deblocking_filter_control_present_flag");
224  if(pcPPS->getDeblockingFilterControlPresentFlag())
225  {
226    WRITE_FLAG( pcPPS->getDeblockingFilterOverrideEnabledFlag() ? 1 : 0,  "deblocking_filter_override_enabled_flag" );
227    WRITE_FLAG( pcPPS->getPicDisableDeblockingFilterFlag() ? 1 : 0,       "pps_disable_deblocking_filter_flag" );
228    if(!pcPPS->getPicDisableDeblockingFilterFlag())
229    {
230      WRITE_SVLC( pcPPS->getDeblockingFilterBetaOffsetDiv2(),             "pps_beta_offset_div2" );
231      WRITE_SVLC( pcPPS->getDeblockingFilterTcOffsetDiv2(),               "pps_tc_offset_div2" );
232    }
233  }
234  WRITE_FLAG( pcPPS->getScalingListPresentFlag() ? 1 : 0,                          "pps_scaling_list_data_present_flag" );
235  if( pcPPS->getScalingListPresentFlag() )
236  {
237    codeScalingList( pcPPS->getScalingList() );
238  }
239#if PPS_FIX_DEPTH
240  if( pcPPS->getSPS()->getVPS()->getDepthId(pcPPS->getSPS()->getLayerId()) )
241  {
242    WRITE_FLAG( 1, "lists_modification_present_flag" );
243  }
244  else
245#endif
246  WRITE_FLAG( pcPPS->getListsModificationPresentFlag(), "lists_modification_present_flag");
247  WRITE_UVLC( pcPPS->getLog2ParallelMergeLevelMinus2(), "log2_parallel_merge_level_minus2");
248  WRITE_FLAG( pcPPS->getSliceHeaderExtensionPresentFlag() ? 1 : 0, "slice_segment_header_extension_present_flag");
249#if !NH_MV
250  Bool pps_extension_present_flag=false;
251  Bool pps_extension_flags[NUM_PPS_EXTENSION_FLAGS]={false};
252
253  pps_extension_flags[PPS_EXT__REXT] = pcPPS->getPpsRangeExtension().settingsDifferFromDefaults(pcPPS->getUseTransformSkip());
254
255  // Other PPS extension flags checked here.
256
257  for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
258  {
259    pps_extension_present_flag|=pps_extension_flags[i];
260  }
261
262  WRITE_FLAG( (pps_extension_present_flag?1:0), "pps_extension_present_flag" );
263
264  if (pps_extension_present_flag)
265  {
266#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
267    static const char *syntaxStrings[]={ "pps_range_extension_flag",
268                                         "pps_multilayer_extension_flag",
269                                         "pps_extension_6bits[0]",
270                                         "pps_extension_6bits[1]",
271                                         "pps_extension_6bits[2]",
272                                         "pps_extension_6bits[3]",
273                                         "pps_extension_6bits[4]",
274                                         "pps_extension_6bits[5]" };
275#endif
276
277    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
278    {
279      WRITE_FLAG( pps_extension_flags[i]?1:0, syntaxStrings[i] );
280    }
281
282    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
283    {
284      if (pps_extension_flags[i])
285      {
286        switch (PPSExtensionFlagIndex(i))
287        {
288          case PPS_EXT__REXT:
289            {
290              const TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
291            if (pcPPS->getUseTransformSkip())
292            {
293                WRITE_UVLC( ppsRangeExtension.getLog2MaxTransformSkipBlockSize()-2,            "log2_max_transform_skip_block_size_minus2");
294            }
295
296              WRITE_FLAG((ppsRangeExtension.getCrossComponentPredictionEnabledFlag() ? 1 : 0), "cross_component_prediction_enabled_flag" );
297
298              WRITE_FLAG(UInt(ppsRangeExtension.getChromaQpOffsetListEnabledFlag()),           "chroma_qp_offset_list_enabled_flag" );
299              if (ppsRangeExtension.getChromaQpOffsetListEnabledFlag())
300            {
301                WRITE_UVLC(ppsRangeExtension.getDiffCuChromaQpOffsetDepth(),                   "diff_cu_chroma_qp_offset_depth");
302                WRITE_UVLC(ppsRangeExtension.getChromaQpOffsetListLen() - 1,                   "chroma_qp_offset_list_len_minus1");
303              /* skip zero index */
304                for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx < ppsRangeExtension.getChromaQpOffsetListLen(); cuChromaQpOffsetIdx++)
305              {
306                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CbOffset,     "cb_qp_offset_list[i]");
307                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CrOffset,     "cr_qp_offset_list[i]");
308              }
309            }
310
311              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_LUMA),           "log2_sao_offset_scale_luma"   );
312              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA),         "log2_sao_offset_scale_chroma" );
313            }
314            break;
315          default:
316            assert(pps_extension_flags[i]==false); // Should never get here with an active PPS extension flag.
317            break;
318        } // switch
319      } // if flag present
320    } // loop over PPS flags
321  } // pps_extension_present_flag is non-zero
322#else
323    WRITE_FLAG( 1, "pps_extension_present_flag" );
324
325    WRITE_FLAG( pcPPS->getPpsRangeExtensionsFlag( ) ? 1 : 0 , "pps_range_extensions_flag" );
326    WRITE_FLAG( pcPPS->getPpsMultilayerExtensionFlag( ) ? 1 : 0 , "pps_multilayer_extension_flag" );
327    WRITE_FLAG( pcPPS->getPps3dExtensionFlag( ) ? 1 : 0 , "pps_3d_extension_flag" );
328    WRITE_CODE( pcPPS->getPpsExtension5bits( ), 5, "pps_extension_5bits" );
329    if ( pcPPS->getPpsRangeExtensionsFlag() )
330    { 
331              const TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
332              if (pcPPS->getUseTransformSkip())
333              {
334                WRITE_UVLC( ppsRangeExtension.getLog2MaxTransformSkipBlockSize()-2,            "log2_max_transform_skip_block_size_minus2");
335              }
336
337              WRITE_FLAG((ppsRangeExtension.getCrossComponentPredictionEnabledFlag() ? 1 : 0), "cross_component_prediction_enabled_flag" );
338
339              WRITE_FLAG(UInt(ppsRangeExtension.getChromaQpOffsetListEnabledFlag()),           "chroma_qp_offset_list_enabled_flag" );
340              if (ppsRangeExtension.getChromaQpOffsetListEnabledFlag())
341              {
342                WRITE_UVLC(ppsRangeExtension.getDiffCuChromaQpOffsetDepth(),                   "diff_cu_chroma_qp_offset_depth");
343                WRITE_UVLC(ppsRangeExtension.getChromaQpOffsetListLen() - 1,                   "chroma_qp_offset_list_len_minus1");
344                /* skip zero index */
345                for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx < ppsRangeExtension.getChromaQpOffsetListLen(); cuChromaQpOffsetIdx++)
346                {
347                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CbOffset,     "cb_qp_offset_list[i]");
348                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CrOffset,     "cr_qp_offset_list[i]");
349                }
350              }
351
352              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_LUMA),           "log2_sao_offset_scale_luma"   );
353              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA),         "log2_sao_offset_scale_chroma" );
354    }
355
356    if ( pcPPS->getPpsMultilayerExtensionFlag() )
357    { 
358      codePpsMultilayerExtension( pcPPS );
359    }
360
361#if NH_3D
362    if( pcPPS->getPps3dExtensionFlag( )  ) // This probably needs to be aligned with Rext and SHVC
363    {
364      codePps3dExtension( pcPPS ); 
365    }
366#endif
367#endif
368  xWriteRbspTrailingBits();
369}
370
371#if NH_3D
372Void  TEncCavlc::codePps3dExtension        ( const TComPPS* pcPPS )
373{
374#if NH_3D_DLT
375  WRITE_FLAG( pcPPS->getDLT()->getDltPresentFlag() ? 1 : 0, "dlt_present_flag" );
376
377  if ( pcPPS->getDLT()->getDltPresentFlag() )
378  {
379    WRITE_CODE(pcPPS->getDLT()->getNumDepthViews() - 1, 6, "pps_depth_layers_minus1");
380    WRITE_CODE((pcPPS->getDLT()->getDepthViewBitDepth() - 8), 4, "pps_bit_depth_for_depth_views_minus8");
381   
382    for( Int i = 0; i <= pcPPS->getDLT()->getNumDepthViews(); i++ )
383    {
384      Int layerId = pcPPS->getDLT()->getDepthIdxToLayerId(i);
385     
386      WRITE_FLAG( pcPPS->getDLT()->getUseDLTFlag( layerId ) ? 1 : 0, "dlt_flag[layerId]" );
387     
388      if ( pcPPS->getDLT()->getUseDLTFlag( layerId ) )
389      {
390        std::vector<Int> aiIdx2DepthValue_coded(256, 0);
391        UInt uiNumDepthValues_coded = pcPPS->getDLT()->getNumDepthValues(layerId);
392       
393        // ----------------------------- Actual coding -----------------------------
394        WRITE_FLAG( pcPPS->getDLT()->getInterViewDltPredEnableFlag( layerId ) ? 1 : 0, "inter_view_dlt_pred_enable_flag[ layerId ]");
395        if ( pcPPS->getDLT()->getInterViewDltPredEnableFlag( layerId ) == false )
396        {
397          WRITE_FLAG( pcPPS->getDLT()->getUseBitmapRep( layerId ) ? 1 : 0, "dlt_bit_map_rep_flag[ layerId ]" );
398         
399          for( UInt ui = 0; ui<uiNumDepthValues_coded; ui++ )
400          {
401            aiIdx2DepthValue_coded[ui] = pcPPS->getDLT()->idx2DepthValue(layerId, ui);
402          }
403        }
404        else
405        {
406          AOF( layerId > 1 );
407          // assumes ref layer id to be 1
408          std::vector<Int> viRefDLT = pcPPS->getDLT()->idx2DepthValue( 1 );
409          UInt uiRefNum = pcPPS->getDLT()->getNumDepthValues( 1 );
410          pcPPS->getDLT()->getDeltaDLT(layerId, viRefDLT, uiRefNum, aiIdx2DepthValue_coded, uiNumDepthValues_coded);
411        }
412       
413        // bit map coding
414        if ( pcPPS->getDLT()->getUseBitmapRep( layerId ) )
415        {
416          UInt uiDltArrayIndex = 0;
417          for (UInt d=0; d < 256; d++)
418          {
419            if ( d == aiIdx2DepthValue_coded[uiDltArrayIndex] )
420            {
421              WRITE_FLAG(1, "dlt_bit_map_flag[ layerId ][ j ]");
422              uiDltArrayIndex++;
423            }
424            else
425            {
426              WRITE_FLAG(0, "dlt_bit_map_flag[ layerId ][ j ]");
427            }
428          }
429        }
430        // Diff Coding
431        else
432        {
433          UInt uiMaxDiff               = 0;
434          UInt uiMinDiff               = MAX_INT;
435          UInt uiLengthMinDiff         = 0;
436          UInt uiLengthDltDiffMinusMin = 0;
437         
438          std::vector<UInt> puiDltDiffValues(uiNumDepthValues_coded, 0);
439         
440          for (UInt d = 1; d < uiNumDepthValues_coded; d++)
441          {
442            puiDltDiffValues[d] = aiIdx2DepthValue_coded[d] - aiIdx2DepthValue_coded[d-1];
443           
444            if ( uiMaxDiff < puiDltDiffValues[d] )
445            {
446              uiMaxDiff = puiDltDiffValues[d];
447            }
448           
449            if ( uiMinDiff > puiDltDiffValues[d] )
450            {
451              uiMinDiff = puiDltDiffValues[d];
452            }
453          }
454         
455          if ( uiNumDepthValues_coded > 2 )
456          {
457            uiLengthMinDiff    = (UInt) gCeilLog2(uiMaxDiff + 1);
458          }
459          if (uiMaxDiff > uiMinDiff)
460          {
461            uiLengthDltDiffMinusMin = (UInt) gCeilLog2(uiMaxDiff - uiMinDiff + 1);
462          }
463         
464          WRITE_CODE(uiNumDepthValues_coded, 8, "num_depth_values_in_dlt[layerId]");    // num_entry
465          {
466            // The condition if( uiNumDepthValues_coded > 0 ) is always true since for Single-view Diff Coding, there is at least one depth value in depth component.
467            if ( uiNumDepthValues_coded > 1 )
468            {
469              WRITE_CODE(uiMaxDiff, 8, "max_diff[ layerId ]");        // max_diff
470            }
471           
472            if ( uiNumDepthValues_coded > 2 )
473            {
474              WRITE_CODE((uiMinDiff - 1), uiLengthMinDiff, "min_diff_minus1[ layerId ]");     // min_diff_minus1
475            }
476           
477            WRITE_CODE(aiIdx2DepthValue_coded[0], 8, "dlt_depth_value0[layerId]");          // entry0
478           
479            if (uiMaxDiff > uiMinDiff)
480            {
481              for (UInt d=1; d < uiNumDepthValues_coded; d++)
482              {
483                WRITE_CODE( (puiDltDiffValues[d] - uiMinDiff), uiLengthDltDiffMinusMin, "dlt_depth_value_diff_minus_min[ layerId ][ j ]");    // entry_value_diff_minus_min[ k ]
484              }
485            }
486          }
487         
488        }
489      }
490    }
491  }
492#endif
493}
494#endif
495
496Void TEncCavlc::codeVUI( const TComVUI *pcVUI, const TComSPS* pcSPS )
497{
498#if ENC_DEC_TRACE
499  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
500#endif
501  WRITE_FLAG(pcVUI->getAspectRatioInfoPresentFlag(),            "aspect_ratio_info_present_flag");
502  if (pcVUI->getAspectRatioInfoPresentFlag())
503  {
504    WRITE_CODE(pcVUI->getAspectRatioIdc(), 8,                   "aspect_ratio_idc" );
505    if (pcVUI->getAspectRatioIdc() == 255)
506    {
507      WRITE_CODE(pcVUI->getSarWidth(), 16,                      "sar_width");
508      WRITE_CODE(pcVUI->getSarHeight(), 16,                     "sar_height");
509    }
510  }
511  WRITE_FLAG(pcVUI->getOverscanInfoPresentFlag(),               "overscan_info_present_flag");
512  if (pcVUI->getOverscanInfoPresentFlag())
513  {
514    WRITE_FLAG(pcVUI->getOverscanAppropriateFlag(),             "overscan_appropriate_flag");
515  }
516  WRITE_FLAG(pcVUI->getVideoSignalTypePresentFlag(),            "video_signal_type_present_flag");
517#if NH_MV
518  assert( pcSPS->getLayerId() == 0 || !pcVUI->getVideoSignalTypePresentFlag() ); 
519#endif
520  if (pcVUI->getVideoSignalTypePresentFlag())
521  {
522    WRITE_CODE(pcVUI->getVideoFormat(), 3,                      "video_format");
523    WRITE_FLAG(pcVUI->getVideoFullRangeFlag(),                  "video_full_range_flag");
524    WRITE_FLAG(pcVUI->getColourDescriptionPresentFlag(),        "colour_description_present_flag");
525    if (pcVUI->getColourDescriptionPresentFlag())
526    {
527      WRITE_CODE(pcVUI->getColourPrimaries(), 8,                "colour_primaries");
528      WRITE_CODE(pcVUI->getTransferCharacteristics(), 8,        "transfer_characteristics");
529      WRITE_CODE(pcVUI->getMatrixCoefficients(), 8,             "matrix_coeffs");
530    }
531  }
532
533  WRITE_FLAG(pcVUI->getChromaLocInfoPresentFlag(),              "chroma_loc_info_present_flag");
534  if (pcVUI->getChromaLocInfoPresentFlag())
535  {
536    WRITE_UVLC(pcVUI->getChromaSampleLocTypeTopField(),         "chroma_sample_loc_type_top_field");
537    WRITE_UVLC(pcVUI->getChromaSampleLocTypeBottomField(),      "chroma_sample_loc_type_bottom_field");
538  }
539
540  WRITE_FLAG(pcVUI->getNeutralChromaIndicationFlag(),           "neutral_chroma_indication_flag");
541  WRITE_FLAG(pcVUI->getFieldSeqFlag(),                          "field_seq_flag");
542  WRITE_FLAG(pcVUI->getFrameFieldInfoPresentFlag(),             "frame_field_info_present_flag");
543
544  Window defaultDisplayWindow = pcVUI->getDefaultDisplayWindow();
545  WRITE_FLAG(defaultDisplayWindow.getWindowEnabledFlag(),       "default_display_window_flag");
546  if( defaultDisplayWindow.getWindowEnabledFlag() )
547  {
548    WRITE_UVLC(defaultDisplayWindow.getWindowLeftOffset()  / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc()), "def_disp_win_left_offset");
549    WRITE_UVLC(defaultDisplayWindow.getWindowRightOffset() / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc()), "def_disp_win_right_offset");
550    WRITE_UVLC(defaultDisplayWindow.getWindowTopOffset()   / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc()), "def_disp_win_top_offset");
551    WRITE_UVLC(defaultDisplayWindow.getWindowBottomOffset()/ TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc()), "def_disp_win_bottom_offset");
552  }
553  const TimingInfo *timingInfo = pcVUI->getTimingInfo();
554  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vui_timing_info_present_flag");
555  if(timingInfo->getTimingInfoPresentFlag())
556  {
557    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vui_num_units_in_tick");
558    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vui_time_scale");
559    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vui_poc_proportional_to_timing_flag");
560    if(timingInfo->getPocProportionalToTimingFlag())
561    {
562      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vui_num_ticks_poc_diff_one_minus1");
563    }
564    WRITE_FLAG(pcVUI->getHrdParametersPresentFlag(),              "vui_hrd_parameters_present_flag");
565    if( pcVUI->getHrdParametersPresentFlag() )
566    {
567      codeHrdParameters(pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
568    }
569  }
570
571  WRITE_FLAG(pcVUI->getBitstreamRestrictionFlag(),              "bitstream_restriction_flag");
572  if (pcVUI->getBitstreamRestrictionFlag())
573  {
574    WRITE_FLAG(pcVUI->getTilesFixedStructureFlag(),             "tiles_fixed_structure_flag");
575    WRITE_FLAG(pcVUI->getMotionVectorsOverPicBoundariesFlag(),  "motion_vectors_over_pic_boundaries_flag");
576    WRITE_FLAG(pcVUI->getRestrictedRefPicListsFlag(),           "restricted_ref_pic_lists_flag");
577    WRITE_UVLC(pcVUI->getMinSpatialSegmentationIdc(),           "min_spatial_segmentation_idc");
578    WRITE_UVLC(pcVUI->getMaxBytesPerPicDenom(),                 "max_bytes_per_pic_denom");
579    WRITE_UVLC(pcVUI->getMaxBitsPerMinCuDenom(),                "max_bits_per_min_cu_denom");
580    WRITE_UVLC(pcVUI->getLog2MaxMvLengthHorizontal(),           "log2_max_mv_length_horizontal");
581    WRITE_UVLC(pcVUI->getLog2MaxMvLengthVertical(),             "log2_max_mv_length_vertical");
582  }
583}
584
585Void TEncCavlc::codeHrdParameters( const TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1 )
586{
587  if( commonInfPresentFlag )
588  {
589    WRITE_FLAG( hrd->getNalHrdParametersPresentFlag() ? 1 : 0 ,  "nal_hrd_parameters_present_flag" );
590    WRITE_FLAG( hrd->getVclHrdParametersPresentFlag() ? 1 : 0 ,  "vcl_hrd_parameters_present_flag" );
591    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
592    {
593      WRITE_FLAG( hrd->getSubPicCpbParamsPresentFlag() ? 1 : 0,  "sub_pic_hrd_params_present_flag" );
594      if( hrd->getSubPicCpbParamsPresentFlag() )
595      {
596        WRITE_CODE( hrd->getTickDivisorMinus2(), 8,              "tick_divisor_minus2" );
597        WRITE_CODE( hrd->getDuCpbRemovalDelayLengthMinus1(), 5,  "du_cpb_removal_delay_increment_length_minus1" );
598        WRITE_FLAG( hrd->getSubPicCpbParamsInPicTimingSEIFlag() ? 1 : 0, "sub_pic_cpb_params_in_pic_timing_sei_flag" );
599        WRITE_CODE( hrd->getDpbOutputDelayDuLengthMinus1(), 5,   "dpb_output_delay_du_length_minus1"  );
600      }
601      WRITE_CODE( hrd->getBitRateScale(), 4,                     "bit_rate_scale" );
602      WRITE_CODE( hrd->getCpbSizeScale(), 4,                     "cpb_size_scale" );
603      if( hrd->getSubPicCpbParamsPresentFlag() )
604      {
605        WRITE_CODE( hrd->getDuCpbSizeScale(), 4,                "du_cpb_size_scale" );
606      }
607      WRITE_CODE( hrd->getInitialCpbRemovalDelayLengthMinus1(), 5, "initial_cpb_removal_delay_length_minus1" );
608      WRITE_CODE( hrd->getCpbRemovalDelayLengthMinus1(),        5, "au_cpb_removal_delay_length_minus1" );
609      WRITE_CODE( hrd->getDpbOutputDelayLengthMinus1(),         5, "dpb_output_delay_length_minus1" );
610    }
611  }
612  Int i, j, nalOrVcl;
613  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
614  {
615    WRITE_FLAG( hrd->getFixedPicRateFlag( i ) ? 1 : 0,          "fixed_pic_rate_general_flag");
616    Bool fixedPixRateWithinCvsFlag = true;
617    if( !hrd->getFixedPicRateFlag( i ) )
618    {
619      fixedPixRateWithinCvsFlag = hrd->getFixedPicRateWithinCvsFlag( i );
620      WRITE_FLAG( hrd->getFixedPicRateWithinCvsFlag( i ) ? 1 : 0, "fixed_pic_rate_within_cvs_flag");
621    }
622    if( fixedPixRateWithinCvsFlag )
623    {
624      WRITE_UVLC( hrd->getPicDurationInTcMinus1( i ),           "elemental_duration_in_tc_minus1");
625    }
626    else
627    {
628      WRITE_FLAG( hrd->getLowDelayHrdFlag( i ) ? 1 : 0,           "low_delay_hrd_flag");
629    }
630    if (!hrd->getLowDelayHrdFlag( i ))
631    {
632      WRITE_UVLC( hrd->getCpbCntMinus1( i ),                      "cpb_cnt_minus1");
633    }
634
635    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
636    {
637      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
638          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
639      {
640        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
641        {
642          WRITE_UVLC( hrd->getBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_value_minus1");
643          WRITE_UVLC( hrd->getCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_value_minus1");
644          if( hrd->getSubPicCpbParamsPresentFlag() )
645          {
646            WRITE_UVLC( hrd->getDuCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_du_value_minus1");
647            WRITE_UVLC( hrd->getDuBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_du_value_minus1");
648          }
649          WRITE_FLAG( hrd->getCbrFlag( i, j, nalOrVcl ) ? 1 : 0, "cbr_flag");
650        }
651      }
652    }
653  }
654}
655
656Void TEncCavlc::codeSPS( const TComSPS* pcSPS )
657{
658
659  const ChromaFormat format                = pcSPS->getChromaFormatIdc();
660  const Bool         chromaEnabled         = isChromaEnabled(format);
661
662#if ENC_DEC_TRACE
663#if H_MV_ENC_DEC_TRAC
664  tracePSHeader( "SPS", pcSPS->getLayerId() ); 
665#else
666  xTraceSPSHeader ();
667#endif
668#endif
669  WRITE_CODE( pcSPS->getVPSId (),          4,       "sps_video_parameter_set_id" );
670#if NH_MV
671  if ( pcSPS->getLayerId() == 0 )
672  {
673#endif
674  WRITE_CODE( pcSPS->getMaxTLayers() - 1,  3,       "sps_max_sub_layers_minus1" );
675#if NH_MV
676  }
677  else
678  {
679    WRITE_CODE( pcSPS->getSpsExtOrMaxSubLayersMinus1( ), 3, "sps_ext_or_max_sub_layers_minus1" );
680  }
681  if ( !pcSPS->getMultiLayerExtSpsFlag() )
682  {
683#endif
684  WRITE_FLAG( pcSPS->getTemporalIdNestingFlag() ? 1 : 0, "sps_temporal_id_nesting_flag" );
685  codePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
686#if NH_MV
687}
688#endif
689  WRITE_UVLC( pcSPS->getSPSId (),                   "sps_seq_parameter_set_id" );
690#if NH_MV
691    if ( pcSPS->getMultiLayerExtSpsFlag() )
692  {
693    WRITE_FLAG( pcSPS->getUpdateRepFormatFlag( ) ? 1 : 0 , "update_rep_format_flag" );
694    if ( pcSPS->getUpdateRepFormatFlag() )
695    { 
696      WRITE_CODE( pcSPS->getSpsRepFormatIdx( ), 8, "sps_rep_format_idx" );
697    }
698  }
699  else
700  {
701#endif
702  WRITE_UVLC( Int(pcSPS->getChromaFormatIdc ()),    "chroma_format_idc" );
703  if( format == CHROMA_444 )
704  {
705    WRITE_FLAG( 0,                                  "separate_colour_plane_flag");
706  }
707
708  WRITE_UVLC( pcSPS->getPicWidthInLumaSamples (),   "pic_width_in_luma_samples" );
709  WRITE_UVLC( pcSPS->getPicHeightInLumaSamples(),   "pic_height_in_luma_samples" );
710  Window conf = pcSPS->getConformanceWindow();
711
712  WRITE_FLAG( conf.getWindowEnabledFlag(),          "conformance_window_flag" );
713  if (conf.getWindowEnabledFlag())
714  {
715    WRITE_UVLC( conf.getWindowLeftOffset()   / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_left_offset" );
716    WRITE_UVLC( conf.getWindowRightOffset()  / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_right_offset" );
717    WRITE_UVLC( conf.getWindowTopOffset()    / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_top_offset" );
718    WRITE_UVLC( conf.getWindowBottomOffset() / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_bottom_offset" );
719  }
720#if NH_MV
721}
722#endif
723#if NH_MV
724  if ( !pcSPS->getMultiLayerExtSpsFlag() )
725  { 
726#endif
727
728  WRITE_UVLC( pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) - 8,                      "bit_depth_luma_minus8" );
729
730  WRITE_UVLC( chromaEnabled ? (pcSPS->getBitDepth(CHANNEL_TYPE_CHROMA) - 8):0,  "bit_depth_chroma_minus8" );
731#if NH_MV
732  }
733#endif
734  WRITE_UVLC( pcSPS->getBitsForPOC()-4,                 "log2_max_pic_order_cnt_lsb_minus4" );
735#if NH_MV
736  if ( !pcSPS->getMultiLayerExtSpsFlag()) 
737  { 
738#endif
739
740  const Bool subLayerOrderingInfoPresentFlag = 1;
741  WRITE_FLAG(subLayerOrderingInfoPresentFlag,       "sps_sub_layer_ordering_info_present_flag");
742  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
743  {
744    WRITE_UVLC( pcSPS->getMaxDecPicBuffering(i) - 1,       "sps_max_dec_pic_buffering_minus1[i]" );
745    WRITE_UVLC( pcSPS->getNumReorderPics(i),               "sps_max_num_reorder_pics[i]" );
746    WRITE_UVLC( pcSPS->getMaxLatencyIncrease(i),           "sps_max_latency_increase_plus1[i]" );
747    if (!subLayerOrderingInfoPresentFlag)
748    {
749      break;
750    }
751  }
752#if NH_MV
753  }
754#endif
755
756  assert( pcSPS->getMaxCUWidth() == pcSPS->getMaxCUHeight() );
757  WRITE_UVLC( pcSPS->getLog2MinCodingBlockSize() - 3,                                "log2_min_luma_coding_block_size_minus3" );
758  WRITE_UVLC( pcSPS->getLog2DiffMaxMinCodingBlockSize(),                             "log2_diff_max_min_luma_coding_block_size" );
759  WRITE_UVLC( pcSPS->getQuadtreeTULog2MinSize() - 2,                                 "log2_min_luma_transform_block_size_minus2" );
760  WRITE_UVLC( pcSPS->getQuadtreeTULog2MaxSize() - pcSPS->getQuadtreeTULog2MinSize(), "log2_diff_max_min_luma_transform_block_size" );
761  WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthInter() - 1,                               "max_transform_hierarchy_depth_inter" );
762  WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthIntra() - 1,                               "max_transform_hierarchy_depth_intra" );
763  WRITE_FLAG( pcSPS->getScalingListFlag() ? 1 : 0,                                   "scaling_list_enabled_flag" );
764  if(pcSPS->getScalingListFlag())
765  {
766#if NH_MV
767    if ( pcSPS->getMultiLayerExtSpsFlag() )
768    {   
769      WRITE_FLAG( pcSPS->getSpsInferScalingListFlag( ) ? 1 : 0 , "sps_infer_scaling_list_flag" );
770    }
771
772    if ( pcSPS->getSpsInferScalingListFlag() )
773    {
774      WRITE_CODE( pcSPS->getSpsScalingListRefLayerId( ), 6, "sps_scaling_list_ref_layer_id" );
775    }
776    else
777    {   
778#endif
779
780    WRITE_FLAG( pcSPS->getScalingListPresentFlag() ? 1 : 0,                          "sps_scaling_list_data_present_flag" );
781    if(pcSPS->getScalingListPresentFlag())
782    {
783      codeScalingList( pcSPS->getScalingList() );
784    }
785#if NH_MV
786    }
787#endif
788  }
789  WRITE_FLAG( pcSPS->getUseAMP() ? 1 : 0,                                            "amp_enabled_flag" );
790  WRITE_FLAG( pcSPS->getUseSAO() ? 1 : 0,                                            "sample_adaptive_offset_enabled_flag");
791
792  WRITE_FLAG( pcSPS->getUsePCM() ? 1 : 0,                                            "pcm_enabled_flag");
793  if( pcSPS->getUsePCM() )
794  {
795    WRITE_CODE( pcSPS->getPCMBitDepth(CHANNEL_TYPE_LUMA) - 1, 4,                            "pcm_sample_bit_depth_luma_minus1" );
796    WRITE_CODE( chromaEnabled ? (pcSPS->getPCMBitDepth(CHANNEL_TYPE_CHROMA) - 1) : 0, 4,    "pcm_sample_bit_depth_chroma_minus1" );
797    WRITE_UVLC( pcSPS->getPCMLog2MinSize() - 3,                                      "log2_min_pcm_luma_coding_block_size_minus3" );
798    WRITE_UVLC( pcSPS->getPCMLog2MaxSize() - pcSPS->getPCMLog2MinSize(),             "log2_diff_max_min_pcm_luma_coding_block_size" );
799    WRITE_FLAG( pcSPS->getPCMFilterDisableFlag()?1 : 0,                              "pcm_loop_filter_disable_flag");
800  }
801
802  assert( pcSPS->getMaxTLayers() > 0 );
803
804  const TComRPSList* rpsList = pcSPS->getRPSList();
805
806  WRITE_UVLC(rpsList->getNumberOfReferencePictureSets(), "num_short_term_ref_pic_sets" );
807  for(Int i=0; i < rpsList->getNumberOfReferencePictureSets(); i++)
808  {
809    const TComReferencePictureSet*rps = rpsList->getReferencePictureSet(i);
810    codeShortTermRefPicSet( rps,false, i);
811  }
812  WRITE_FLAG( pcSPS->getLongTermRefsPresent() ? 1 : 0,         "long_term_ref_pics_present_flag" );
813  if (pcSPS->getLongTermRefsPresent())
814  {
815    WRITE_UVLC(pcSPS->getNumLongTermRefPicSPS(), "num_long_term_ref_pics_sps" );
816    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
817    {
818      WRITE_CODE( pcSPS->getLtRefPicPocLsbSps(k), pcSPS->getBitsForPOC(), "lt_ref_pic_poc_lsb_sps");
819      WRITE_FLAG( pcSPS->getUsedByCurrPicLtSPSFlag(k), "used_by_curr_pic_lt_sps_flag[i]");
820    }
821  }
822  WRITE_FLAG( pcSPS->getTMVPFlagsPresent()  ? 1 : 0,           "sps_temporal_mvp_enable_flag" );
823
824  WRITE_FLAG( pcSPS->getUseStrongIntraSmoothing(),             "sps_strong_intra_smoothing_enable_flag" );
825
826  WRITE_FLAG( pcSPS->getVuiParametersPresentFlag(),             "vui_parameters_present_flag" );
827  if (pcSPS->getVuiParametersPresentFlag())
828  {
829      codeVUI(pcSPS->getVuiParameters(), pcSPS);
830  }
831
832#if !NH_MV
833  Bool sps_extension_present_flag=false;
834  Bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS]={false};
835
836  sps_extension_flags[SPS_EXT__REXT] = pcSPS->getSpsRangeExtension().settingsDifferFromDefaults();
837
838  // Other SPS extension flags checked here.
839
840  for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
841  {
842    sps_extension_present_flag|=sps_extension_flags[i];
843  }
844
845  WRITE_FLAG( (sps_extension_present_flag?1:0), "sps_extension_present_flag" );
846
847  if (sps_extension_present_flag)
848  {
849#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
850    static const char *syntaxStrings[]={ "sps_range_extension_flag",
851      "sps_multilayer_extension_flag",
852      "sps_extension_6bits[0]",
853      "sps_extension_6bits[1]",
854      "sps_extension_6bits[2]",
855      "sps_extension_6bits[3]",
856      "sps_extension_6bits[4]",
857      "sps_extension_6bits[5]" };
858#endif
859
860    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
861    {
862      WRITE_FLAG( sps_extension_flags[i]?1:0, syntaxStrings[i] );
863    }
864
865    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
866    {
867      if (sps_extension_flags[i])
868      {
869        switch (SPSExtensionFlagIndex(i))
870        {
871        case SPS_EXT__REXT:
872          {
873            const TComSPSRExt &spsRangeExtension=pcSPS->getSpsRangeExtension();
874
875            WRITE_FLAG( (spsRangeExtension.getTransformSkipRotationEnabledFlag() ? 1 : 0),      "transform_skip_rotation_enabled_flag");
876            WRITE_FLAG( (spsRangeExtension.getTransformSkipContextEnabledFlag() ? 1 : 0),       "transform_skip_context_enabled_flag");
877            WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT) ? 1 : 0), "implicit_rdpcm_enabled_flag" );
878            WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT) ? 1 : 0), "explicit_rdpcm_enabled_flag" );
879            WRITE_FLAG( (spsRangeExtension.getExtendedPrecisionProcessingFlag() ? 1 : 0),       "extended_precision_processing_flag" );
880            WRITE_FLAG( (spsRangeExtension.getIntraSmoothingDisabledFlag() ? 1 : 0),            "intra_smoothing_disabled_flag" );
881            WRITE_FLAG( (spsRangeExtension.getHighPrecisionOffsetsEnabledFlag() ? 1 : 0),       "high_precision_offsets_enabled_flag" );
882            WRITE_FLAG( (spsRangeExtension.getPersistentRiceAdaptationEnabledFlag() ? 1 : 0),   "persistent_rice_adaptation_enabled_flag" );
883            WRITE_FLAG( (spsRangeExtension.getCabacBypassAlignmentEnabledFlag() ? 1 : 0),       "cabac_bypass_alignment_enabled_flag" );
884          break;
885          }
886        default:
887          assert(sps_extension_flags[i]==false); // Should never get here with an active SPS extension flag.
888          break;
889        }
890      }
891    }
892  }
893#else
894  WRITE_FLAG( pcSPS->getSpsExtensionPresentFlag(), "sps_extension_present_flag" );
895
896  if ( pcSPS->getSpsExtensionPresentFlag() )
897  {
898    WRITE_FLAG( pcSPS->getSpsRangeExtensionsFlag( )     ? 1 : 0 , "sps_range_extensions_flag" );
899    WRITE_FLAG( pcSPS->getSpsMultilayerExtensionFlag( ) ? 1 : 0 , "sps_multilayer_extension_flag" );
900    WRITE_FLAG( pcSPS->getSps3dExtensionFlag( )         ? 1 : 0 , "sps_3d_extension_flag" );
901    WRITE_CODE( pcSPS->getSpsExtension5bits( )              , 5 , "sps_extension_5bits" );
902  }
903
904  if ( pcSPS->getSpsRangeExtensionsFlag() )
905  {
906    const TComSPSRExt &spsRangeExtension=pcSPS->getSpsRangeExtension();
907
908    WRITE_FLAG( (spsRangeExtension.getTransformSkipRotationEnabledFlag() ? 1 : 0),      "transform_skip_rotation_enabled_flag");
909    WRITE_FLAG( (spsRangeExtension.getTransformSkipContextEnabledFlag() ? 1 : 0),       "transform_skip_context_enabled_flag");
910    WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT) ? 1 : 0), "implicit_rdpcm_enabled_flag" );
911    WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT) ? 1 : 0), "explicit_rdpcm_enabled_flag" );
912    WRITE_FLAG( (spsRangeExtension.getExtendedPrecisionProcessingFlag() ? 1 : 0),       "extended_precision_processing_flag" );
913    WRITE_FLAG( (spsRangeExtension.getIntraSmoothingDisabledFlag() ? 1 : 0),            "intra_smoothing_disabled_flag" );
914    WRITE_FLAG( (spsRangeExtension.getHighPrecisionOffsetsEnabledFlag() ? 1 : 0),       "high_precision_offsets_enabled_flag" );
915    WRITE_FLAG( (spsRangeExtension.getPersistentRiceAdaptationEnabledFlag() ? 1 : 0),   "persistent_rice_adaptation_enabled_flag" );
916    WRITE_FLAG( (spsRangeExtension.getCabacBypassAlignmentEnabledFlag() ? 1 : 0),       "cabac_bypass_alignment_enabled_flag" );
917  }
918
919  if ( pcSPS->getSpsMultilayerExtensionFlag() )
920  {
921    codeSPSExtension( pcSPS ); 
922  }
923
924#if NH_3D
925  if ( pcSPS->getSps3dExtensionFlag() )
926  {
927    codeSPS3dExtension( pcSPS ); 
928  }
929
930#endif
931#endif
932  xWriteRbspTrailingBits();
933}
934
935#if NH_MV
936Void TEncCavlc::codeSPSExtension( const TComSPS* pcSPS )
937{
938  WRITE_FLAG( pcSPS->getInterViewMvVertConstraintFlag() ? 1 : 0, "inter_view_mv_vert_constraint_flag" );
939}
940
941
942Void TEncCavlc::codePpsMultilayerExtension(const TComPPS* pcPPS)
943{
944  WRITE_FLAG( pcPPS->getPocResetInfoPresentFlag( ) ? 1 : 0 , "poc_reset_info_present_flag" );
945  WRITE_FLAG( pcPPS->getPpsInferScalingListFlag( ) ? 1 : 0 , "pps_infer_scaling_list_flag" );
946  if (pcPPS->getPpsInferScalingListFlag())
947  {
948    WRITE_CODE( pcPPS->getPpsScalingListRefLayerId( ), 6, "pps_scaling_list_ref_layer_id" );
949  }
950  WRITE_UVLC( 0, "num_ref_loc_offsets" );
951  WRITE_FLAG( 0 , "colour_mapping_enabled_flag" );
952
953}
954
955#endif
956
957#if NH_3D
958Void TEncCavlc::codeSPS3dExtension( const TComSPS* pcSPS )
959{
960  const TComSps3dExtension* sps3dExt = pcSPS->getSps3dExtension();
961  for( Int d = 0; d  <=  1; d++ )
962  {
963    WRITE_FLAG( sps3dExt->getIvMvPredFlag( d ) ? 1 : 0 , "iv_mv_pred_flag" );
964    WRITE_FLAG( sps3dExt->getIvMvScalingFlag( d ) ? 1 : 0 , "iv_mv_scaling_flag" );
965    if( d  ==  0 )
966    {
967      WRITE_UVLC( sps3dExt->getLog2SubPbSizeMinus3( d ), "log2_sub_pb_size_minus3" );
968      WRITE_FLAG( sps3dExt->getIvResPredFlag( d ) ? 1 : 0 , "iv_res_pred_flag" );
969      WRITE_FLAG( sps3dExt->getDepthRefinementFlag( d ) ? 1 : 0 , "depth_refinement_flag" );
970      WRITE_FLAG( sps3dExt->getViewSynthesisPredFlag( d ) ? 1 : 0 , "view_synthesis_pred_flag" );
971      WRITE_FLAG( sps3dExt->getDepthBasedBlkPartFlag( d ) ? 1 : 0 , "depth_based_blk_part_flag" );
972    }
973    else 
974    {
975      WRITE_FLAG( sps3dExt->getMpiFlag( d ) ? 1 : 0 , "mpi_flag" );
976      WRITE_UVLC( sps3dExt->getLog2MpiSubPbSizeMinus3( d ), "log2_mpi_sub_pb_size_minus3" );
977      WRITE_FLAG( sps3dExt->getIntraContourFlag( d ) ? 1 : 0 , "intra_contour_flag" );
978      WRITE_FLAG( sps3dExt->getIntraSdcWedgeFlag( d ) ? 1 : 0 , "intra_sdc_wedge_flag" );
979      WRITE_FLAG( sps3dExt->getQtPredFlag( d ) ? 1 : 0 , "qt_pred_flag" );
980      WRITE_FLAG( sps3dExt->getInterSdcFlag( d ) ? 1 : 0 , "inter_sdc_flag" );
981      WRITE_FLAG( sps3dExt->getDepthIntraSkipFlag( d ) ? 1 : 0 , "intra_skip_flag" );
982    }
983  }
984}
985#endif
986
987
988Void TEncCavlc::codeVPS( const TComVPS* pcVPS )
989{
990#if ENC_DEC_TRACE
991#if H_MV_ENC_DEC_TRAC
992  tracePSHeader( "VPS", getEncTop()->getLayerId() ); 
993#else
994  xTraceVPSHeader();
995#endif
996#endif
997  WRITE_CODE( pcVPS->getVPSId(),                    4,        "vps_video_parameter_set_id" );
998#if NH_MV
999  WRITE_FLAG( pcVPS->getVpsBaseLayerInternalFlag( ) ? 1 : 0 , "vps_base_layer_internal_flag" );
1000  WRITE_FLAG( pcVPS->getVpsBaseLayerAvailableFlag( ) ? 1 : 0 , "vps_base_layer_available_flag" );
1001#else
1002  WRITE_FLAG(                                       1,        "vps_base_layer_internal_flag" );
1003  WRITE_FLAG(                                       1,        "vps_base_layer_available_flag" );
1004#endif
1005#if NH_MV
1006  WRITE_CODE( pcVPS->getMaxLayersMinus1(),       6,        "vps_max_layers_minus1" );
1007#else
1008  WRITE_CODE( 0,                                    6,        "vps_max_layers_minus1" );
1009#endif
1010  WRITE_CODE( pcVPS->getMaxTLayers() - 1,           3,        "vps_max_sub_layers_minus1" );
1011  WRITE_FLAG( pcVPS->getTemporalNestingFlag(),                "vps_temporal_id_nesting_flag" );
1012  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
1013  WRITE_CODE( 0xffff,                              16,        "vps_reserved_0xffff_16bits" );
1014  codePTL( pcVPS->getPTL(), true, pcVPS->getMaxTLayers() - 1 );
1015  const Bool subLayerOrderingInfoPresentFlag = 1;
1016  WRITE_FLAG(subLayerOrderingInfoPresentFlag,              "vps_sub_layer_ordering_info_present_flag");
1017  for(UInt i=0; i <= pcVPS->getMaxTLayers()-1; i++)
1018  {
1019    WRITE_UVLC( pcVPS->getMaxDecPicBuffering(i) - 1,       "vps_max_dec_pic_buffering_minus1[i]" );
1020    WRITE_UVLC( pcVPS->getNumReorderPics(i),               "vps_max_num_reorder_pics[i]" );
1021    WRITE_UVLC( pcVPS->getMaxLatencyIncrease(i),           "vps_max_latency_increase_plus1[i]" );
1022    if (!subLayerOrderingInfoPresentFlag)
1023    {
1024      break;
1025    }
1026  }
1027
1028  assert( pcVPS->getNumHrdParameters() <= MAX_VPS_NUM_HRD_PARAMETERS );
1029#if NH_MV
1030  assert( pcVPS->getVpsMaxLayerId() < MAX_VPS_NUH_LAYER_ID_PLUS1 );
1031  WRITE_CODE( pcVPS->getVpsMaxLayerId(), 6,                 "vps_max_layer_id" ); 
1032  WRITE_UVLC( pcVPS->getVpsNumLayerSetsMinus1(),  "vps_num_layer_sets_minus1" );
1033  for( UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx ++ )
1034  {
1035    // Operation point set
1036    for( UInt i = 0; i <= pcVPS->getVpsMaxLayerId(); i ++ )
1037    {
1038#else
1039  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
1040  WRITE_CODE( pcVPS->getMaxNuhReservedZeroLayerId(), 6,     "vps_max_layer_id" );
1041  WRITE_UVLC( pcVPS->getMaxOpSets() - 1,                    "vps_num_layer_sets_minus1" );
1042  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
1043  {
1044    // Operation point set
1045    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
1046    {
1047      // Only applicable for version 1
1048      // pcVPS->setLayerIdIncludedFlag( true, opsIdx, i );
1049#endif
1050      WRITE_FLAG( pcVPS->getLayerIdIncludedFlag( opsIdx, i ) ? 1 : 0, "layer_id_included_flag[opsIdx][i]" );
1051    }
1052  }
1053  const TimingInfo *timingInfo = pcVPS->getTimingInfo();
1054  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vps_timing_info_present_flag");
1055  if(timingInfo->getTimingInfoPresentFlag())
1056  {
1057    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vps_num_units_in_tick");
1058    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vps_time_scale");
1059    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vps_poc_proportional_to_timing_flag");
1060    if(timingInfo->getPocProportionalToTimingFlag())
1061    {
1062      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vps_num_ticks_poc_diff_one_minus1");
1063    }
1064    WRITE_UVLC( pcVPS->getNumHrdParameters(),                 "vps_num_hrd_parameters" );
1065
1066    if( pcVPS->getNumHrdParameters() > 0 )
1067    {
1068      for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
1069      {
1070        // Only applicable for version 1
1071        WRITE_UVLC( pcVPS->getHrdOpSetIdx( i ),                "hrd_layer_set_idx" );
1072        if( i > 0 )
1073        {
1074          WRITE_FLAG( pcVPS->getCprmsPresentFlag( i ) ? 1 : 0, "cprms_present_flag[i]" );
1075        }
1076        codeHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
1077      }
1078    }
1079  }
1080#if NH_MV
1081  WRITE_FLAG( pcVPS->getVpsExtensionFlag(),                     "vps_extension_flag" );
1082  m_pcBitIf->writeAlignOne();
1083  codeVPSExtension( pcVPS );                           
1084#if NH_3D
1085  WRITE_FLAG( 1,                     "vps_extension2_flag" ); 
1086  WRITE_FLAG( 1,                     "vps_3d_extension_flag" );
1087  m_pcBitIf->writeAlignOne();     
1088  codeVPS3dExtension( pcVPS ); 
1089  WRITE_FLAG( 0,                     "vps_extension3_flag" );
1090#else
1091  WRITE_FLAG( 0,                     "vps_extension2_flag" );
1092#endif
1093#else
1094  WRITE_FLAG( 0,                     "vps_extension_flag" );
1095#endif
1096
1097  //future extensions here..
1098  xWriteRbspTrailingBits();
1099}
1100
1101#if NH_MV
1102Void TEncCavlc::codeVPSExtension( const TComVPS *pcVPS ) 
1103{ 
1104  if( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() )
1105  {
1106    codePTL( pcVPS->getPTL( 1 ),0, pcVPS->getMaxSubLayersMinus1()  );
1107  }
1108
1109  WRITE_FLAG( pcVPS->getSplittingFlag() ? 1 : 0,             "splitting_flag" );
1110
1111  for( Int type = 0; type < MAX_NUM_SCALABILITY_TYPES; type++ )
1112  {
1113    WRITE_FLAG( pcVPS->getScalabilityMaskFlag( type ) ? 1 : 0,   "scalability_mask_flag[i]" );
1114  }
1115
1116  for( Int sIdx = 0; sIdx < pcVPS->getNumScalabilityTypes( ) - ( pcVPS->getSplittingFlag() ? 1 : 0 ); sIdx++ )
1117  {
1118    WRITE_CODE( pcVPS->getDimensionIdLen( sIdx ) - 1 , 3,    "dimension_id_len_minus1[j]");   
1119  }
1120
1121  if ( pcVPS->getSplittingFlag() )
1122  {   
1123    assert( pcVPS->getDimensionIdLen( pcVPS->getNumScalabilityTypes( ) - 1 ) == pcVPS->inferLastDimsionIdLenMinus1() + 1 );
1124  }   
1125
1126  WRITE_FLAG( pcVPS->getVpsNuhLayerIdPresentFlag() ? 1 : 0,  "vps_nuh_layer_id_present_flag");
1127
1128  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1129  {
1130    if ( pcVPS->getVpsNuhLayerIdPresentFlag() )
1131    {     
1132      WRITE_CODE( pcVPS->getLayerIdInNuh( i ), 6,          "layer_id_in_nuh[i]");
1133    }
1134    else
1135    {
1136      assert( pcVPS->getLayerIdInNuh( i ) == i ); 
1137    }
1138
1139    assert(  pcVPS->getLayerIdInVps( pcVPS->getLayerIdInNuh( i ) ) == i ); 
1140
1141    for( Int j = 0; j < pcVPS->getNumScalabilityTypes() ; j++ )
1142    {
1143      if ( !pcVPS->getSplittingFlag() )
1144      {
1145        WRITE_CODE( pcVPS->getDimensionId( i, j ), pcVPS->getDimensionIdLen( j ), "dimension_id[i][j]");     
1146      }
1147      else
1148      {
1149        assert( pcVPS->getDimensionId( i, j ) ==  pcVPS->inferDimensionId( i, j )  );
1150      }
1151    }
1152  }
1153
1154  WRITE_CODE( pcVPS->getViewIdLen( ), 4, "view_id_len" );
1155
1156  if ( pcVPS->getViewIdLen( ) > 0 )
1157  {   
1158    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1159    {
1160      WRITE_CODE( pcVPS->getViewIdVal( i ), pcVPS->getViewIdLen( ), "view_id_val[i]" );
1161    }
1162  }
1163  else
1164  {
1165    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1166    {
1167      assert( pcVPS->getViewIdVal( i ) == 0 ); 
1168    }
1169  }
1170
1171
1172  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1173  {
1174    for( Int j = 0; j < i; j++ )
1175    {
1176      WRITE_FLAG( pcVPS->getDirectDependencyFlag( i, j ),    "direct_dependency_flag[i][j]" );
1177    }
1178  }
1179
1180  if ( pcVPS->getNumIndependentLayers() > 1 ) 
1181  {
1182    WRITE_UVLC( pcVPS->getNumAddLayerSets( ), "num_add_layer_sets" );
1183  }
1184  for (Int i = 0; i < pcVPS->getNumAddLayerSets(); i++)
1185  {
1186    for (Int j = 1; j < pcVPS->getNumIndependentLayers(); j++)
1187    {
1188      WRITE_CODE( pcVPS->getHighestLayerIdxPlus1( i, j ), pcVPS->getHighestLayerIdxPlus1Len( j )  , "highest_layer_idx_plus1" );
1189    }
1190 
1191  }
1192
1193
1194  WRITE_FLAG( pcVPS->getVpsSubLayersMaxMinus1PresentFlag( ) ? 1 : 0 , "vps_sub_layers_max_minus1_present_flag" );
1195  if ( pcVPS->getVpsSubLayersMaxMinus1PresentFlag() )
1196  {
1197    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1198    {
1199      WRITE_CODE( pcVPS->getSubLayersVpsMaxMinus1( i ), 3, "sub_layers_vps_max_minus1" );
1200      pcVPS->checkSubLayersVpsMaxMinus1( i ); 
1201    }
1202  } 
1203  else
1204  {
1205    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1206    {
1207      assert( pcVPS->getSubLayersVpsMaxMinus1( i ) + 1 == pcVPS->getMaxTLayers( ) );   
1208    }
1209  }
1210  WRITE_FLAG( pcVPS->getMaxTidRefPresentFlag( ) ? 1 : 0 , "max_tid_ref_present_flag" );
1211
1212  if ( pcVPS->getMaxTidRefPresentFlag() )
1213  {   
1214    for( Int i = 0; i < pcVPS->getMaxLayersMinus1(); i++ )
1215    {
1216      for( Int j = i + 1; j <= pcVPS->getMaxLayersMinus1(); j++ )
1217      {
1218        if ( pcVPS->getDirectDependencyFlag(j,i) )
1219        {
1220          WRITE_CODE( pcVPS->getMaxTidIlRefPicsPlus1( i, j ), 3, "max_tid_il_ref_pics_plus1" );
1221        }
1222      }
1223    }
1224  }
1225
1226  WRITE_FLAG( pcVPS->getAllRefLayersActiveFlag( ) ? 1 : 0 , "all_ref_layers_active_flag" );
1227  WRITE_UVLC( pcVPS->getVpsNumProfileTierLevelMinus1( ), "vps_num_profile_tier_level_minus1" );
1228
1229  Int offsetVal =  ( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() ) ? 2 : 1;   
1230  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 2 : 1; i <= pcVPS->getVpsNumProfileTierLevelMinus1(); i++ )
1231  {
1232    WRITE_FLAG( pcVPS->getVpsProfilePresentFlag( i ) ? 1 : 0, "vps_profile_present_flag[i]" );
1233    codePTL( pcVPS->getPTL( offsetVal ), pcVPS->getVpsProfilePresentFlag( i ), pcVPS->getMaxTLayers() - 1 );
1234    offsetVal++;
1235  }
1236
1237
1238  if (pcVPS->getNumLayerSets() > 1)
1239  {
1240    WRITE_UVLC( pcVPS->getNumAddOlss( ), "num_add_olss" );
1241    WRITE_CODE( pcVPS->getDefaultOutputLayerIdc( ), 2, "default_output_layer_idc" );
1242  }
1243
1244  assert( pcVPS->getOutputLayerFlag(0, 0) == pcVPS->inferOutputLayerFlag( 0, 0 )); 
1245  assert( pcVPS->getLayerSetIdxForOlsMinus1( 0 ) == -1 ); 
1246
1247
1248  if (pcVPS->getVpsBaseLayerInternalFlag() )
1249  { 
1250    assert( pcVPS->getProfileTierLevelIdx(0,0) == pcVPS->inferProfileTierLevelIdx(0,0) );
1251  }
1252
1253
1254  for( Int i = 1; i < pcVPS->getNumOutputLayerSets( ); i++ )
1255  {
1256    if( pcVPS->getNumLayerSets() > 2 && i >= pcVPS->getNumLayerSets( ) )   
1257    {     
1258      WRITE_CODE( pcVPS->getLayerSetIdxForOlsMinus1( i ), pcVPS->getLayerSetIdxForOlsMinus1Len( i ) ,      "layer_set_idx_for_ols_minus1[i]" );
1259    }
1260
1261    if ( i > pcVPS->getVpsNumLayerSetsMinus1() || pcVPS->getDefaultOutputLayerIdc() == 2 )
1262    {       
1263      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1264      {
1265        WRITE_FLAG( pcVPS->getOutputLayerFlag( i, j) ? 1 : 0, "output_layer_flag" );
1266      }             
1267    }
1268    else
1269    {
1270      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ) - 1; j++ )
1271      {             
1272        assert( pcVPS->getOutputLayerFlag( i , j ) == pcVPS->inferOutputLayerFlag( i, j )); 
1273      }
1274    }       
1275       
1276    for ( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx(i)); j++ )
1277    {   
1278      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() > 0 )
1279      {
1280        WRITE_CODE( pcVPS->getProfileTierLevelIdx( i, j ), pcVPS->getProfileTierLevelIdxLen() ,"profile_tier_level_idx[ i ][ j ]" );   
1281      }
1282      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() == 0 )
1283      {
1284        assert( pcVPS->getProfileTierLevelIdx( i , j ) == pcVPS->inferProfileTierLevelIdx( i, j ) );
1285      }
1286
1287    }
1288    if( pcVPS->getNumOutputLayersInOutputLayerSet( i ) == 1 && pcVPS->getNumDirectRefLayers( pcVPS->getOlsHighestOutputLayerId( i ) ) > 0 )
1289    {
1290      WRITE_FLAG( pcVPS->getAltOutputLayerFlag( i ) ? 1 : 0 , "alt_output_layer_flag[ i ]" );
1291    }
1292  }
1293
1294  WRITE_UVLC( pcVPS->getVpsNumRepFormatsMinus1( ), "vps_num_rep_formats_minus1" );
1295
1296  for (Int i = 0; i <= pcVPS->getVpsNumRepFormatsMinus1(); i++ )
1297  {   
1298    const TComRepFormat* curRepFormat = pcVPS->getRepFormat(i);     
1299    const TComRepFormat* prevRepFormat = i > 0 ? pcVPS->getRepFormat( i - 1) : NULL; 
1300    codeRepFormat( i, curRepFormat ,  prevRepFormat); 
1301  }
1302
1303  if ( pcVPS->getVpsNumRepFormatsMinus1() > 0 )
1304  {
1305    WRITE_FLAG( pcVPS->getRepFormatIdxPresentFlag( ) ? 1 : 0 , "rep_format_idx_present_flag" );
1306  }
1307
1308  if( pcVPS->getRepFormatIdxPresentFlag() ) 
1309  {
1310    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1311    {
1312        WRITE_CODE( pcVPS->getVpsRepFormatIdx(i), pcVPS->getVpsRepFormatIdxLen(), "vps_rep_format_idx[i]" );
1313    }
1314  }
1315  else
1316  {
1317    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1318    {
1319      assert( pcVPS->getVpsRepFormatIdx( i ) ==  pcVPS->inferVpsRepFormatIdx( i ) );
1320    }
1321  }
1322
1323  WRITE_FLAG( pcVPS->getMaxOneActiveRefLayerFlag( ) ? 1 : 0, "max_one_active_ref_layer_flag" );
1324  WRITE_FLAG( pcVPS->getVpsPocLsbAlignedFlag( ) ? 1 : 0 , "vps_poc_lsb_aligned_flag" );
1325  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1326  {
1327    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) )  ==  0 )
1328    {     
1329      WRITE_FLAG( pcVPS->getPocLsbNotPresentFlag( i ) ? 1 : 0 , "poc_lsb_not_present_flag" );
1330    }
1331  }
1332
1333  codeDpbSize( pcVPS ); 
1334
1335  WRITE_UVLC( pcVPS->getDirectDepTypeLenMinus2 ( ),         "direct_dep_type_len_minus2"); 
1336
1337  WRITE_FLAG( pcVPS->getDefaultDirectDependencyFlag( ) ? 1 : 0 , "default_direct_dependency_flag" );
1338  if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1339  { 
1340    WRITE_CODE( pcVPS->getDefaultDirectDependencyType( ), pcVPS->getDirectDepTypeLenMinus2( ) + 2 , "default_direct_dependency_type" );   
1341  }
1342
1343  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ?  1 : 2; i <= pcVPS->getMaxLayersMinus1(); i++ )
1344  {
1345    for( Int j = pcVPS->getVpsBaseLayerInternalFlag() ?  0 : 1; j < i; j++ )
1346    {
1347      if (pcVPS->getDirectDependencyFlag( i, j) )
1348      {       
1349        if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1350        { 
1351          assert( pcVPS->getDirectDependencyType( i, j ) == pcVPS->getDefaultDirectDependencyType( ) );
1352        }
1353        else
1354        {
1355          assert ( pcVPS->getDirectDependencyType( i, j ) != -1 ); 
1356          WRITE_CODE( pcVPS->getDirectDependencyType( i, j ),pcVPS->getDirectDepTypeLenMinus2( ) + 2,  "direct_dependency_type[i][j]" );
1357        }
1358      }
1359    }
1360  } 
1361  WRITE_UVLC( 0, "vps_non_vui_extension_length" );
1362 
1363
1364
1365
1366  WRITE_FLAG( pcVPS->getVpsVuiPresentFlag() ? 1 : 0 , "vps_vui_present_flag" );
1367  if( pcVPS->getVpsVuiPresentFlag() )
1368  {
1369    m_pcBitIf->writeAlignOne();  // vps_vui_alignment_bit_equal_to_one
1370    codeVPSVUI( pcVPS ); 
1371  }     
1372  else
1373  {
1374    const TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1375    assert( pcVPSVUI ); 
1376    assert( !pcVPSVUI->getCrossLayerIrapAlignedFlag() );     
1377  }
1378
1379}
1380
1381Void TEncCavlc::codeVideoSignalInfo( const TComVideoSignalInfo* pcVideoSignalInfo )
1382{
1383  assert( pcVideoSignalInfo ); 
1384  WRITE_CODE( pcVideoSignalInfo->getVideoVpsFormat( ), 3, "video_vps_format" );
1385  WRITE_FLAG( pcVideoSignalInfo->getVideoFullRangeVpsFlag( ) ? 1 : 0 , "video_full_range_vps_flag" );
1386  WRITE_CODE( pcVideoSignalInfo->getColourPrimariesVps( ), 8, "colour_primaries_vps" );
1387  WRITE_CODE( pcVideoSignalInfo->getTransferCharacteristicsVps( ), 8, "transfer_characteristics_vps" );
1388  WRITE_CODE( pcVideoSignalInfo->getMatrixCoeffsVps( ), 8, "matrix_coeffs_vps" );
1389}
1390
1391Void TEncCavlc::codeDpbSize( const TComVPS* vps )
1392{ 
1393  const TComDpbSize * dpbSize = vps->getDpbSize(); 
1394
1395  for( Int i = 1; i < vps->getNumOutputLayerSets(); i++ )
1396  { 
1397    Int currLsIdx = vps->olsIdxToLsIdx( i ); 
1398    WRITE_FLAG( dpbSize->getSubLayerFlagInfoPresentFlag( i ) ? 1 : 0 , "sub_layer_flag_info_present_flag" );
1399    for( Int j = 0; j  <=  vps->getMaxSubLayersInLayerSetMinus1( currLsIdx ); j++ )
1400    { 
1401      if( j > 0  &&  dpbSize->getSubLayerDpbInfoPresentFlag( i, j )  ) 
1402      {
1403        WRITE_FLAG( dpbSize->getSubLayerDpbInfoPresentFlag( i, j ) ? 1 : 0 , "sub_layer_dpb_info_present_flag" );
1404      }
1405      if( dpbSize->getSubLayerDpbInfoPresentFlag( i, j ) )
1406      { 
1407        for( Int k = 0; k < vps->getNumLayersInIdList( currLsIdx ); k++ )   
1408        {
1409          if ( vps->getNecessaryLayerFlag( i, k ) && ( vps->getVpsBaseLayerInternalFlag() || ( vps->getLayerSetLayerIdList(vps->olsIdxToLsIdx(i),k) != 0 ) ))
1410          {
1411            WRITE_UVLC( dpbSize->getMaxVpsDecPicBufferingMinus1( i, k, j ), "max_vps_dec_pic_buffering_minus1" );
1412          }
1413          else
1414          {
1415            if ( vps->getNecessaryLayerFlag( i, k ) && ( j == 0 ) && ( k == 0 ) ) 
1416            {
1417              assert( dpbSize->getMaxVpsDecPicBufferingMinus1(i ,k, j ) ==  0 );
1418            }
1419          }
1420        }
1421        WRITE_UVLC( dpbSize->getMaxVpsNumReorderPics( i, j ), "max_vps_num_reorder_pics" );
1422        WRITE_UVLC( dpbSize->getMaxVpsLatencyIncreasePlus1( i, j ), "max_vps_latency_increase_plus1" );
1423      }
1424      else
1425      {
1426        if ( j > 0 )
1427        {
1428          for( Int k = 0; k < vps->getNumLayersInIdList( vps->olsIdxToLsIdx( i ) ); k++ )   
1429          {
1430            if ( vps->getNecessaryLayerFlag(i, k ) )
1431            {           
1432              assert( dpbSize->getMaxVpsDecPicBufferingMinus1( i, k, j ) == dpbSize->getMaxVpsDecPicBufferingMinus1( i,k, j - 1 ) );
1433            }
1434          }
1435          assert( dpbSize->getMaxVpsNumReorderPics      ( i, j ) ==  dpbSize->getMaxVpsNumReorderPics      ( i, j - 1 ) );
1436          assert( dpbSize->getMaxVpsLatencyIncreasePlus1( i, j ) ==  dpbSize->getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
1437        }
1438      }
1439    }
1440  }
1441}
1442
1443Void TEncCavlc::codeRepFormat( Int i, const TComRepFormat* pcRepFormat, const TComRepFormat* pcPrevRepFormat )
1444{
1445  assert( pcRepFormat ); 
1446
1447  WRITE_CODE( pcRepFormat->getPicWidthVpsInLumaSamples( ),  16, "pic_width_vps_in_luma_samples" );
1448  WRITE_CODE( pcRepFormat->getPicHeightVpsInLumaSamples( ), 16, "pic_height_vps_in_luma_samples" );
1449  WRITE_FLAG( pcRepFormat->getChromaAndBitDepthVpsPresentFlag( ) ? 1 : 0 , "chroma_and_bit_depth_vps_present_flag" );
1450 
1451  pcRepFormat->checkChromaAndBitDepthVpsPresentFlag( i ); 
1452
1453  if ( pcRepFormat->getChromaAndBitDepthVpsPresentFlag() )
1454  { 
1455    WRITE_CODE( pcRepFormat->getChromaFormatVpsIdc( ), 2, "chroma_format_vps_idc" ); 
1456
1457    if ( pcRepFormat->getChromaFormatVpsIdc() == 3 )
1458    {
1459      WRITE_FLAG( pcRepFormat->getSeparateColourPlaneVpsFlag( ) ? 1 : 0 , "separate_colour_plane_vps_flag" );
1460    }
1461    WRITE_CODE( pcRepFormat->getBitDepthVpsLumaMinus8( ),      4, "bit_depth_vps_luma_minus8" );
1462    WRITE_CODE( pcRepFormat->getBitDepthVpsChromaMinus8( ),    4, "bit_depth_vps_chroma_minus8" );
1463  }
1464  else
1465  {
1466    pcRepFormat->checkInferChromaAndBitDepth(pcPrevRepFormat ); 
1467  }
1468  WRITE_FLAG( pcRepFormat->getConformanceWindowVpsFlag( ) ? 1 : 0 , "conformance_window_vps_flag" );
1469  if ( pcRepFormat->getConformanceWindowVpsFlag() )
1470  {   
1471    WRITE_UVLC( pcRepFormat->getConfWinVpsLeftOffset( ), "conf_win_vps_left_offset" );
1472    WRITE_UVLC( pcRepFormat->getConfWinVpsRightOffset( ), "conf_win_vps_right_offset" );
1473    WRITE_UVLC( pcRepFormat->getConfWinVpsTopOffset( ), "conf_win_vps_top_offset" );
1474    WRITE_UVLC( pcRepFormat->getConfWinVpsBottomOffset( ), "conf_win_vps_bottom_offset" );
1475  }
1476}
1477
1478Void TEncCavlc::codeVPSVUI( const TComVPS* pcVPS )
1479{
1480  assert( pcVPS ); 
1481
1482  const TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1483
1484  assert( pcVPSVUI ); 
1485
1486
1487  WRITE_FLAG( pcVPSVUI->getCrossLayerPicTypeAlignedFlag( ) ? 1 : 0 , "cross_layer_pic_type_aligned_flag" );
1488  if ( !pcVPSVUI->getCrossLayerPicTypeAlignedFlag() )
1489  { 
1490    WRITE_FLAG( pcVPSVUI->getCrossLayerIrapAlignedFlag( ) ? 1 : 0 , "cross_layer_irap_aligned_flag" );
1491  }
1492  if( pcVPSVUI->getCrossLayerIrapAlignedFlag( ) )
1493  {
1494    WRITE_FLAG( pcVPSVUI->getAllLayersIdrAlignedFlag( ) ? 1 : 0 , "all_layers_idr_aligned_flag" );
1495  }
1496  WRITE_FLAG( pcVPSVUI->getBitRatePresentVpsFlag( ) ? 1 : 0 , "bit_rate_present_vps_flag" );
1497  WRITE_FLAG( pcVPSVUI->getPicRatePresentVpsFlag( ) ? 1 : 0 , "pic_rate_present_vps_flag" );
1498  if( pcVPSVUI->getBitRatePresentVpsFlag( )  ||  pcVPSVUI->getPicRatePresentVpsFlag( ) )
1499  {
1500    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i < pcVPS->getNumLayerSets(); i++ )
1501    {
1502      for( Int j = 0; j  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( i ); j++ ) 
1503      {
1504        if( pcVPSVUI->getBitRatePresentVpsFlag( ) )
1505        {
1506          WRITE_FLAG( pcVPSVUI->getBitRatePresentFlag( i, j ) ? 1 : 0 , "bit_rate_present_flag" );
1507        }
1508        if( pcVPSVUI->getBitRatePresentVpsFlag( )  )
1509        {
1510          WRITE_FLAG( pcVPSVUI->getPicRatePresentFlag( i, j ) ? 1 : 0 , "pic_rate_present_flag" );
1511        }
1512        if( pcVPSVUI->getBitRatePresentFlag( i, j ) )
1513        {
1514          WRITE_CODE( pcVPSVUI->getAvgBitRate( i, j ), 16, "avg_bit_rate" );
1515          WRITE_CODE( pcVPSVUI->getMaxBitRate( i, j ), 16, "max_bit_rate" );
1516        }
1517        if( pcVPSVUI->getPicRatePresentFlag( i, j ) )
1518        {
1519          WRITE_CODE( pcVPSVUI->getConstantPicRateIdc( i, j ), 2, "constant_pic_rate_idc" );
1520          WRITE_CODE( pcVPSVUI->getAvgPicRate( i, j ), 16, "avg_pic_rate" );
1521        }
1522      }
1523    }
1524  }
1525
1526  WRITE_FLAG( pcVPSVUI->getVideoSignalInfoIdxPresentFlag( ) ? 1 : 0 , "video_signal_info_idx_present_flag" );
1527  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() )
1528  {
1529    WRITE_CODE( pcVPSVUI->getVpsNumVideoSignalInfoMinus1( ), 4, "vps_num_video_signal_info_minus1" );
1530  }
1531  else
1532  {
1533    assert( pcVPSVUI->getVpsNumVideoSignalInfoMinus1() == pcVPS->getMaxLayersMinus1() - ( pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1)  ); 
1534  }
1535
1536  for( Int i = 0; i <= pcVPSVUI->getVpsNumVideoSignalInfoMinus1(); i++ )
1537  {
1538    assert( pcVPSVUI->getVideoSignalInfo( i ) != NULL ); 
1539    const TComVideoSignalInfo* curVideoSignalInfo = pcVPSVUI->getVideoSignalInfo( i ); 
1540    codeVideoSignalInfo( curVideoSignalInfo );     
1541
1542  }
1543
1544  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() && pcVPSVUI->getVpsNumVideoSignalInfoMinus1() > 0 )
1545  {
1546    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1547    {
1548      WRITE_CODE( pcVPSVUI->getVpsVideoSignalInfoIdx( i ), 4, "vps_video_signal_info_idx" );
1549    }
1550  }
1551  WRITE_FLAG( pcVPSVUI->getTilesNotInUseFlag( ) ? 1 : 0 , "tiles_not_in_use_flag" );
1552  if( !pcVPSVUI->getTilesNotInUseFlag() ) 
1553  {     
1554    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1555    {
1556      WRITE_FLAG( pcVPSVUI->getTilesInUseFlag( i ) ? 1 : 0 , "tiles_in_use_flag[i]" );
1557      if( pcVPSVUI->getTilesInUseFlag( i ) ) 
1558      {
1559        WRITE_FLAG( pcVPSVUI->getLoopFilterNotAcrossTilesFlag( i ) ? 1 : 0, "loop_filter_not_across_tiles_flag[i]" );
1560      }
1561    } 
1562    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 2; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1563    {
1564      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ) ; j++ )
1565      { 
1566        Int layerIdx = pcVPS->getLayerIdInVps(pcVPS->getIdRefLayer(pcVPS->getLayerIdInNuh( i ) , j  )); 
1567        if( pcVPSVUI->getTilesInUseFlag( i )  &&  pcVPSVUI->getTilesInUseFlag( layerIdx ) ) 
1568        {
1569          WRITE_FLAG( pcVPSVUI->getTileBoundariesAlignedFlag( i, j ) ? 1 : 0 , "tile_boundaries_aligned_flag[i][j]" );
1570        }
1571      } 
1572    }
1573  } 
1574
1575  WRITE_FLAG( pcVPSVUI->getWppNotInUseFlag( ) ? 1 : 0 , "wpp_not_in_use_flag" );
1576
1577  if( !pcVPSVUI->getWppNotInUseFlag( ) )
1578  {
1579    for( Int i = 0; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1580    {
1581      WRITE_FLAG( pcVPSVUI->getWppInUseFlag( i ) ? 1 : 0 , "wpp_in_use_flag[i]" );
1582    }
1583  }
1584  WRITE_FLAG( pcVPSVUI->getSingleLayerForNonIrapFlag( ) ? 1 : 0 , "single_layer_for_non_irap_flag" );
1585  WRITE_FLAG( pcVPSVUI->getHigherLayerIrapSkipFlag( ) ? 1 : 0 , "higher_layer_irap_skip_flag" );
1586  WRITE_FLAG( pcVPSVUI->getIlpRestrictedRefLayersFlag( ) ? 1 : 0 , "ilp_restricted_ref_layers_flag" );
1587
1588  if( pcVPSVUI->getIlpRestrictedRefLayersFlag( ) )
1589  {
1590    for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1591    {
1592      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ); j++ )
1593      {
1594        if( pcVPS->getVpsBaseLayerInternalFlag() || pcVPS->getIdRefLayer( pcVPS->getLayerIdInNuh( i ), j ) > 0 )
1595        {       
1596          WRITE_UVLC( pcVPSVUI->getMinSpatialSegmentOffsetPlus1( i, j ), "min_spatial_segment_offset_plus1" );
1597          if( pcVPSVUI->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
1598          {
1599            WRITE_FLAG( pcVPSVUI->getCtuBasedOffsetEnabledFlag( i, j ) ? 1 : 0 , "ctu_based_offset_enabled_flag" );
1600            if( pcVPSVUI->getCtuBasedOffsetEnabledFlag( i, j ) )
1601            {
1602              WRITE_UVLC( pcVPSVUI->getMinHorizontalCtuOffsetPlus1( i, j ), "min_horizontal_ctu_offset_plus1" );
1603            }
1604          }
1605        }
1606      }
1607    }
1608  }
1609
1610
1611  WRITE_FLAG( pcVPSVUI->getVpsVuiBspHrdPresentFlag( ) ? 1 : 0 , "vps_vui_bsp_hrd_present_flag" );
1612  if ( pcVPSVUI->getVpsVuiBspHrdPresentFlag( ) )
1613  {
1614    codeVpsVuiBspHrdParameters( pcVPS ); 
1615  }
1616  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1617  {
1618    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i )) == 0 ) 
1619    {
1620      WRITE_FLAG( pcVPSVUI->getBaseLayerParameterSetCompatibilityFlag( i ) ? 1 : 0 , "base_layer_parameter_set_compatibility_flag" );
1621    }
1622  }
1623}
1624
1625Void TEncCavlc::codeVpsVuiBspHrdParameters( const TComVPS* pcVPS )
1626{
1627  assert( pcVPS ); 
1628
1629  const TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1630
1631  assert( pcVPSVUI ); 
1632
1633  const TComVpsVuiBspHrdParameters*  vpsVuiBspHrdP = pcVPSVUI->getVpsVuiBspHrdParameters(); 
1634
1635  assert ( vpsVuiBspHrdP );
1636 
1637  WRITE_UVLC( vpsVuiBspHrdP->getVpsNumAddHrdParams( ), "vps_num_add_hrd_params" );
1638
1639  for( Int i = pcVPS->getNumHrdParameters(); i < pcVPS->getNumHrdParameters() + vpsVuiBspHrdP->getVpsNumAddHrdParams(); i++ )
1640  { 
1641    if( i > 0 ) 
1642    {
1643      WRITE_FLAG( vpsVuiBspHrdP->getCprmsAddPresentFlag( i ) ? 1 : 0 , "cprms_add_present_flag" );
1644    }
1645    WRITE_UVLC( vpsVuiBspHrdP->getNumSubLayerHrdMinus1( i ), "num_sub_layer_hrd_minus1" );
1646    const TComHRD* hrdParameters = vpsVuiBspHrdP->getHrdParametermeters( i ); 
1647    codeHrdParameters( hrdParameters, vpsVuiBspHrdP->getCprmsAddPresentFlag( i ), vpsVuiBspHrdP->getNumSubLayerHrdMinus1( i ) );     
1648  }
1649
1650  for( Int h = 1; h < pcVPS->getNumOutputLayerSets(); h++ )
1651  { 
1652    WRITE_UVLC( vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ), "num_signalled_partitioning_schemes" );
1653
1654    for( Int j = 1; j < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; j++ )
1655    { 
1656      WRITE_UVLC( vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ), "num_partitions_in_scheme_minus1" );
1657      for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ ) 
1658      {
1659        for( Int r = 0; r < pcVPS->getNumLayersInIdList(pcVPS->olsIdxToLsIdx( h ) )   ; r++ ) 
1660        {
1661          WRITE_FLAG( vpsVuiBspHrdP->getLayerIncludedInPartitionFlag( h, j, k, r ) ? 1 : 0 , "layer_included_in_partition_flag" );
1662        }
1663      }
1664    } 
1665    for( Int i = 0; i < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; i++ ) 
1666    {
1667      for( Int t = 0; t  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( pcVPS->olsIdxToLsIdx( h ) ); t++ )
1668      { 
1669        WRITE_UVLC( vpsVuiBspHrdP->getNumBspSchedulesMinus1( h, i, t ), "num_bsp_schedules_minus1" );
1670        for( Int j = 0; j  <=  vpsVuiBspHrdP->getNumBspSchedulesMinus1( h, i, t ); j++ ) 
1671        {
1672          for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ )
1673          { 
1674            WRITE_CODE( vpsVuiBspHrdP->getBspHrdIdx( h, i, t, j, k ), vpsVuiBspHrdP->getBspHrdIdxLen( pcVPS ), "bsp_hrd_idx" );
1675            WRITE_UVLC( vpsVuiBspHrdP->getBspSchedIdx( h, i, t, j, k ), "bsp_sched_idx" );
1676          } 
1677        }
1678      } 
1679    }
1680  } 
1681} 
1682#endif
1683
1684#if NH_3D
1685Void TEncCavlc::codeVPS3dExtension( const TComVPS* pcVPS )
1686{ 
1687  WRITE_UVLC( pcVPS->getCpPrecision( ), "cp_precision" );
1688  for (Int n = 1; n < pcVPS->getNumViews(); n++)
1689  {
1690    Int i      = pcVPS->getViewOIdxList( n );
1691    Int iInVps = pcVPS->getVoiInVps( i ); 
1692    WRITE_CODE( pcVPS->getNumCp( iInVps ), 6, "num_cp" );
1693
1694    if( pcVPS->getNumCp( iInVps ) > 0 )
1695    {
1696      WRITE_FLAG( pcVPS->getCpInSliceSegmentHeaderFlag( iInVps ) ? 1 : 0 , "cp_in_slice_segment_header_flag" );
1697      for( Int m = 0; m < pcVPS->getNumCp( iInVps ); m++ )
1698      {
1699        WRITE_UVLC( pcVPS->getCpRefVoi( iInVps, m ), "cp_ref_voi" );
1700        if( !pcVPS->getCpInSliceSegmentHeaderFlag( iInVps ) )
1701        {
1702          Int j      = pcVPS->getCpRefVoi( iInVps, m );
1703          Int jInVps = pcVPS->getVoiInVps( j ); 
1704          WRITE_SVLC( pcVPS->getVpsCpScale   ( iInVps, jInVps ), "vps_cp_scale" );
1705          WRITE_SVLC( pcVPS->getVpsCpOff     ( iInVps, jInVps ), "vps_cp_off" );
1706          WRITE_SVLC( pcVPS->getVpsCpInvScale( iInVps, jInVps ) + pcVPS->getVpsCpScale( iInVps, jInVps ), "vps_cp_inv_scale_plus_scale" );
1707          WRITE_SVLC( pcVPS->getVpsCpInvOff  ( iInVps, jInVps ) + pcVPS->getVpsCpOff  ( iInVps, jInVps ), "vps_cp_inv_off_plus_off" );
1708        }
1709      }
1710    }
1711  } 
1712}
1713#endif
1714
1715
1716Void TEncCavlc::codeSliceHeader         ( TComSlice* pcSlice )
1717{
1718#if NH_MV
1719  const TComVPS* vps = pcSlice->getVPS(); 
1720#endif
1721
1722#if ENC_DEC_TRACE
1723#if NH_MV
1724  tracePSHeader( "Slice", pcSlice->getLayerId() ); 
1725#else
1726  xTraceSliceHeader ();
1727#endif
1728#endif
1729
1730  const ChromaFormat format                = pcSlice->getSPS()->getChromaFormatIdc();
1731  const UInt         numberValidComponents = getNumberValidComponents(format);
1732  const Bool         chromaEnabled         = isChromaEnabled(format);
1733
1734  //calculate number of bits required for slice address
1735  Int maxSliceSegmentAddress = pcSlice->getPic()->getNumberOfCtusInFrame();
1736  Int bitsSliceSegmentAddress = 0;
1737  while(maxSliceSegmentAddress>(1<<bitsSliceSegmentAddress))
1738  {
1739    bitsSliceSegmentAddress++;
1740  }
1741  const Int ctuTsAddress = pcSlice->getSliceSegmentCurStartCtuTsAddr();
1742
1743  //write slice address
1744  const Int sliceSegmentRsAddress = pcSlice->getPic()->getPicSym()->getCtuTsToRsAddrMap(ctuTsAddress);
1745
1746  WRITE_FLAG( sliceSegmentRsAddress==0, "first_slice_segment_in_pic_flag" );
1747  if ( pcSlice->getRapPicFlag() )
1748  {
1749    WRITE_FLAG( pcSlice->getNoOutputPriorPicsFlag() ? 1 : 0, "no_output_of_prior_pics_flag" );
1750  }
1751#if PPS_FIX_DEPTH
1752  if( pcSlice->getIsDepth() )
1753  {
1754    WRITE_UVLC( 1, "slice_pic_parameter_set_id" );
1755  }
1756  else
1757#endif
1758
1759  WRITE_UVLC( pcSlice->getPPS()->getPPSId(), "slice_pic_parameter_set_id" );
1760  if ( pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag() && (sliceSegmentRsAddress!=0) )
1761  {
1762    WRITE_FLAG( pcSlice->getDependentSliceSegmentFlag() ? 1 : 0, "dependent_slice_segment_flag" );
1763  }
1764  if(sliceSegmentRsAddress>0)
1765  {
1766    WRITE_CODE( sliceSegmentRsAddress, bitsSliceSegmentAddress, "slice_segment_address" );
1767  }
1768  if ( !pcSlice->getDependentSliceSegmentFlag() )
1769  {
1770
1771#if NH_MV   
1772    Int esb = 0;  //Don't use i, otherwise will shadow something below
1773
1774    if ( pcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
1775    {
1776      esb++; 
1777      WRITE_FLAG( pcSlice->getDiscardableFlag( ) ? 1 : 0 , "discardable_flag" );
1778      if (pcSlice->getDiscardableFlag( ))
1779      {
1780        assert(pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
1781          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
1782          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
1783          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
1784          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
1785      }
1786    }
1787
1788    if ( pcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
1789    {
1790      esb++; 
1791      WRITE_FLAG( pcSlice->getCrossLayerBlaFlag( ) ? 1 : 0 , "cross_layer_bla_flag" );
1792    }
1793    pcSlice->checkCrossLayerBlaFlag( ); 
1794
1795    for (; esb < pcSlice->getPPS()->getNumExtraSliceHeaderBits(); esb++)   
1796#else
1797    for (Int i = 0; i < pcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
1798#endif
1799    {
1800      WRITE_FLAG(0, "slice_reserved_flag[]");
1801    }
1802
1803
1804
1805    WRITE_UVLC( pcSlice->getSliceType(),       "slice_type" );
1806
1807    if( pcSlice->getPPS()->getOutputFlagPresentFlag() )
1808    {
1809      WRITE_FLAG( pcSlice->getPicOutputFlag() ? 1 : 0, "pic_output_flag" );
1810    }
1811
1812#if NH_MV
1813    if ( (pcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( pcSlice->getLayerIdInVps())) || !pcSlice->getIdrPicFlag() )
1814    {
1815      Int picOrderCntLSB = (pcSlice->getPOC()-pcSlice->getLastIDR()+(1<<pcSlice->getSPS()->getBitsForPOC())) & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
1816      WRITE_CODE( picOrderCntLSB, pcSlice->getSPS()->getBitsForPOC(), "slice_pic_order_cnt_lsb");
1817      pcSlice->setSlicePicOrderCntLsb( picOrderCntLSB ); 
1818    }
1819
1820#endif
1821
1822    if( !pcSlice->getIdrPicFlag() )
1823    {
1824#if !NH_MV
1825      Int picOrderCntLSB = (pcSlice->getPOC()-pcSlice->getLastIDR()+(1<<pcSlice->getSPS()->getBitsForPOC())) & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
1826      WRITE_CODE( picOrderCntLSB, pcSlice->getSPS()->getBitsForPOC(), "slice_pic_order_cnt_lsb");
1827#endif
1828      const TComReferencePictureSet* rps = pcSlice->getRPS();
1829
1830      // check for bitstream restriction stating that:
1831      // If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
1832      // Ideally this process should not be repeated for each slice in a picture
1833      if (pcSlice->isIRAP())
1834      {
1835        for (Int picIdx = 0; picIdx < rps->getNumberOfPictures(); picIdx++)
1836        {
1837          assert (!rps->getUsed(picIdx));
1838        }
1839      }
1840
1841      if(pcSlice->getRPSidx() < 0)
1842      {
1843        WRITE_FLAG( 0, "short_term_ref_pic_set_sps_flag");
1844        codeShortTermRefPicSet( rps, true, pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets());
1845      }
1846      else
1847      {
1848        WRITE_FLAG( 1, "short_term_ref_pic_set_sps_flag");
1849        Int numBits = 0;
1850        while ((1 << numBits) < pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
1851        {
1852          numBits++;
1853        }
1854        if (numBits > 0)
1855        {
1856          WRITE_CODE( pcSlice->getRPSidx(), numBits, "short_term_ref_pic_set_idx" );
1857        }
1858      }
1859      if(pcSlice->getSPS()->getLongTermRefsPresent())
1860      {
1861        Int numLtrpInSH = rps->getNumberOfLongtermPictures();
1862        Int ltrpInSPS[MAX_NUM_REF_PICS];
1863        Int numLtrpInSPS = 0;
1864        UInt ltrpIndex;
1865        Int counter = 0;
1866        // WARNING: The following code only works only if a matching long-term RPS is
1867        //          found in the SPS for ALL long-term pictures
1868        //          The problem is that the SPS coded long-term pictures are moved to the
1869        //          beginning of the list which causes a mismatch when no reference picture
1870        //          list reordering is used
1871        //          NB: Long-term coding is currently not supported in general by the HM encoder
1872        for(Int k = rps->getNumberOfPictures()-1; k > rps->getNumberOfPictures()-rps->getNumberOfLongtermPictures()-1; k--)
1873        {
1874          if (findMatchingLTRP(pcSlice, &ltrpIndex, rps->getPOC(k), rps->getUsed(k)))
1875          {
1876            ltrpInSPS[numLtrpInSPS] = ltrpIndex;
1877            numLtrpInSPS++;
1878          }
1879          else
1880          {
1881            counter++;
1882          }
1883        }
1884        numLtrpInSH -= numLtrpInSPS;
1885        // check that either all long-term pictures are coded in SPS or in slice header (no mixing)
1886        assert (numLtrpInSH==0 || numLtrpInSPS==0); 
1887
1888        Int bitsForLtrpInSPS = 0;
1889        while (pcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
1890        {
1891          bitsForLtrpInSPS++;
1892        }
1893        if (pcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
1894        {
1895          WRITE_UVLC( numLtrpInSPS, "num_long_term_sps");
1896        }
1897        WRITE_UVLC( numLtrpInSH, "num_long_term_pics");
1898        // Note that the LSBs of the LT ref. pic. POCs must be sorted before.
1899        // Not sorted here because LT ref indices will be used in setRefPicList()
1900        Int prevDeltaMSB = 0, prevLSB = 0;
1901        Int offset = rps->getNumberOfNegativePictures() + rps->getNumberOfPositivePictures();
1902        counter = 0;
1903        // Warning: If some pictures are moved to ltrpInSPS, i is referring to a wrong index
1904        //          (mapping would be required)
1905        for(Int i=rps->getNumberOfPictures()-1 ; i > offset-1; i--, counter++)
1906        {
1907          if (counter < numLtrpInSPS)
1908          {
1909            if (bitsForLtrpInSPS > 0)
1910            {
1911              WRITE_CODE( ltrpInSPS[counter], bitsForLtrpInSPS, "lt_idx_sps[i]");
1912            }
1913          }
1914          else
1915          {
1916            WRITE_CODE( rps->getPocLSBLT(i), pcSlice->getSPS()->getBitsForPOC(), "poc_lsb_lt");
1917            WRITE_FLAG( rps->getUsed(i), "used_by_curr_pic_lt_flag");
1918          }
1919          WRITE_FLAG( rps->getDeltaPocMSBPresentFlag(i), "delta_poc_msb_present_flag");
1920
1921          if(rps->getDeltaPocMSBPresentFlag(i))
1922          {
1923            Bool deltaFlag = false;
1924            //  First LTRP from SPS                 ||  First LTRP from SH                              || curr LSB            != prev LSB
1925            if( (i == rps->getNumberOfPictures()-1) || (i == rps->getNumberOfPictures()-1-numLtrpInSPS) || (rps->getPocLSBLT(i) != prevLSB) )
1926            {
1927              deltaFlag = true;
1928            }
1929            if(deltaFlag)
1930            {
1931              WRITE_UVLC( rps->getDeltaPocMSBCycleLT(i), "delta_poc_msb_cycle_lt[i]" );
1932            }
1933            else
1934            {
1935              Int differenceInDeltaMSB = rps->getDeltaPocMSBCycleLT(i) - prevDeltaMSB;
1936              assert(differenceInDeltaMSB >= 0);
1937              WRITE_UVLC( differenceInDeltaMSB, "delta_poc_msb_cycle_lt[i]" );
1938            }
1939            prevLSB = rps->getPocLSBLT(i);
1940            prevDeltaMSB = rps->getDeltaPocMSBCycleLT(i);
1941          }
1942        }
1943      }
1944      if (pcSlice->getSPS()->getTMVPFlagsPresent())
1945      {
1946#if NH_MV
1947        WRITE_FLAG( pcSlice->getEnableTMVPFlag() ? 1 : 0, "slice_temporal_mvp_enabled_flag" );
1948#else
1949        WRITE_FLAG( pcSlice->getEnableTMVPFlag() ? 1 : 0, "slice_temporal_mvp_enabled_flag" );
1950#endif
1951      }
1952    }
1953#if NH_MV
1954    Bool interLayerPredLayerIdcPresentFlag = false; 
1955    Int layerId = pcSlice->getLayerId(); 
1956#if NH_3D
1957    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumRefListLayers( layerId ) > 0 )
1958#else
1959    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumDirectRefLayers( layerId ) > 0 )
1960#endif
1961    {   
1962      WRITE_FLAG( pcSlice->getInterLayerPredEnabledFlag( ) ? 1 : 0 , "inter_layer_pred_enabled_flag" );
1963#if NH_3D
1964      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumRefListLayers( layerId ) > 1 )
1965#else
1966      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerId ) > 1 )
1967#endif
1968      {           
1969        if( !vps->getMaxOneActiveRefLayerFlag()) 
1970        {
1971          WRITE_CODE( pcSlice->getNumInterLayerRefPicsMinus1( ), pcSlice->getNumInterLayerRefPicsMinus1Len( ), "num_inter_layer_ref_pics_minus1" );
1972        }
1973#if NH_3D
1974        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumRefListLayers( layerId ) )
1975#else
1976        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumDirectRefLayers( layerId ) )
1977#endif
1978        {       
1979          interLayerPredLayerIdcPresentFlag = true; 
1980          for( Int idx = 0; idx < pcSlice->getNumActiveRefLayerPics(); idx++ )   
1981          {
1982            WRITE_CODE( pcSlice->getInterLayerPredLayerIdc( idx ), pcSlice->getInterLayerPredLayerIdcLen( ), "inter_layer_pred_layer_idc" );
1983          }
1984        }
1985      } 
1986    }
1987    if ( !interLayerPredLayerIdcPresentFlag )
1988    {
1989      for( Int i = 0; i < pcSlice->getNumActiveRefLayerPics(); i++ )   
1990      {
1991        assert( pcSlice->getInterLayerPredLayerIdc( i ) == pcSlice->getRefLayerPicIdc( i ) );
1992      }
1993    }
1994#endif
1995
1996#if NH_3D     
1997  if( getEncTop()->decProcAnnexI() )
1998  {
1999      if ( pcSlice->getInCmpPredAvailFlag() )
2000      {
2001        WRITE_FLAG( pcSlice->getInCompPredFlag(), "in_comp_pred_flag" );
2002      }
2003  }
2004#endif
2005
2006    if(pcSlice->getSPS()->getUseSAO())
2007    {
2008       WRITE_FLAG( pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA), "slice_sao_luma_flag" );
2009       if (chromaEnabled)
2010       {
2011         WRITE_FLAG( pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA), "slice_sao_chroma_flag" );
2012       }
2013    }
2014
2015    //check if numrefidxes match the defaults. If not, override
2016
2017    if (!pcSlice->isIntra())
2018    {
2019      Bool overrideFlag = (pcSlice->getNumRefIdx( REF_PIC_LIST_0 )!=pcSlice->getPPS()->getNumRefIdxL0DefaultActive()||(pcSlice->isInterB()&&pcSlice->getNumRefIdx( REF_PIC_LIST_1 )!=pcSlice->getPPS()->getNumRefIdxL1DefaultActive()));
2020#if PPS_FIX_DEPTH
2021      overrideFlag |= (pcSlice->getIsDepth() && !pcSlice->getViewIndex());
2022#endif
2023      WRITE_FLAG( overrideFlag ? 1 : 0,                               "num_ref_idx_active_override_flag");
2024      if (overrideFlag)
2025      {
2026        WRITE_UVLC( pcSlice->getNumRefIdx( REF_PIC_LIST_0 ) - 1,      "num_ref_idx_l0_active_minus1" );
2027        if (pcSlice->isInterB())
2028        {
2029          WRITE_UVLC( pcSlice->getNumRefIdx( REF_PIC_LIST_1 ) - 1,    "num_ref_idx_l1_active_minus1" );
2030        }
2031        else
2032        {
2033          pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
2034        }
2035      }
2036    }
2037    else
2038    {
2039      pcSlice->setNumRefIdx(REF_PIC_LIST_0, 0);
2040      pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
2041    }
2042#if PPS_FIX_DEPTH
2043    if( (pcSlice->getPPS()->getListsModificationPresentFlag() || (pcSlice->getIsDepth() && !pcSlice->getViewIndex())) && pcSlice->getNumRpsCurrTempList() > 1)
2044#else
2045    if( pcSlice->getPPS()->getListsModificationPresentFlag() && pcSlice->getNumRpsCurrTempList() > 1)
2046#endif
2047    {
2048      TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
2049      if(!pcSlice->isIntra())
2050      {
2051        WRITE_FLAG(pcSlice->getRefPicListModification()->getRefPicListModificationFlagL0() ? 1 : 0,       "ref_pic_list_modification_flag_l0" );
2052        if (pcSlice->getRefPicListModification()->getRefPicListModificationFlagL0())
2053        {
2054          Int numRpsCurrTempList0 = pcSlice->getNumRpsCurrTempList();
2055          if (numRpsCurrTempList0 > 1)
2056          {
2057            Int length = 1;
2058            numRpsCurrTempList0 --;
2059            while ( numRpsCurrTempList0 >>= 1)
2060            {
2061              length ++;
2062            }
2063            for(Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_0 ); i++)
2064            {
2065              WRITE_CODE( refPicListModification->getRefPicSetIdxL0(i), length, "list_entry_l0");
2066            }
2067          }
2068        }
2069      }
2070      if(pcSlice->isInterB())
2071      {
2072        WRITE_FLAG(pcSlice->getRefPicListModification()->getRefPicListModificationFlagL1() ? 1 : 0,       "ref_pic_list_modification_flag_l1" );
2073        if (pcSlice->getRefPicListModification()->getRefPicListModificationFlagL1())
2074        {
2075          Int numRpsCurrTempList1 = pcSlice->getNumRpsCurrTempList();
2076          if ( numRpsCurrTempList1 > 1 )
2077          {
2078            Int length = 1;
2079            numRpsCurrTempList1 --;
2080            while ( numRpsCurrTempList1 >>= 1)
2081            {
2082              length ++;
2083            }
2084            for(Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_1 ); i++)
2085            {
2086              WRITE_CODE( refPicListModification->getRefPicSetIdxL1(i), length, "list_entry_l1");
2087            }
2088          }
2089        }
2090      }
2091    }
2092
2093    if (pcSlice->isInterB())
2094    {
2095      WRITE_FLAG( pcSlice->getMvdL1ZeroFlag() ? 1 : 0,   "mvd_l1_zero_flag");
2096    }
2097
2098    if(!pcSlice->isIntra())
2099    {
2100      if (!pcSlice->isIntra() && pcSlice->getPPS()->getCabacInitPresentFlag())
2101      {
2102        SliceType sliceType   = pcSlice->getSliceType();
2103        SliceType  encCABACTableIdx = pcSlice->getEncCABACTableIdx();
2104        Bool encCabacInitFlag = (sliceType!=encCABACTableIdx && encCABACTableIdx!=I_SLICE) ? true : false;
2105        pcSlice->setCabacInitFlag( encCabacInitFlag );
2106        WRITE_FLAG( encCabacInitFlag?1:0, "cabac_init_flag" );
2107      }
2108    }
2109
2110    if ( pcSlice->getEnableTMVPFlag() )
2111    {
2112      if ( pcSlice->getSliceType() == B_SLICE )
2113      {
2114        WRITE_FLAG( pcSlice->getColFromL0Flag(), "collocated_from_l0_flag" );
2115      }
2116
2117      if ( pcSlice->getSliceType() != I_SLICE &&
2118        ((pcSlice->getColFromL0Flag()==1 && pcSlice->getNumRefIdx(REF_PIC_LIST_0)>1)||
2119        (pcSlice->getColFromL0Flag()==0  && pcSlice->getNumRefIdx(REF_PIC_LIST_1)>1)))
2120      {
2121        WRITE_UVLC( pcSlice->getColRefIdx(), "collocated_ref_idx" );
2122      }
2123    }
2124    if ( (pcSlice->getPPS()->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pcSlice->getPPS()->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
2125    {
2126      xCodePredWeightTable( pcSlice );
2127    }
2128#if H_3D_IC
2129    else if( pcSlice->getViewIndex() && ( pcSlice->getSliceType() == P_SLICE || pcSlice->getSliceType() == B_SLICE )
2130      && !pcSlice->getIsDepth() && vps->getNumRefListLayers( layerId ) > 0 
2131      && getEncTop()->decProcAnnexI()       
2132      )
2133    {
2134      WRITE_FLAG( pcSlice->getApplyIC() ? 1 : 0, "slice_ic_enable_flag" );
2135      if( pcSlice->getApplyIC() )
2136      {
2137        WRITE_FLAG( pcSlice->getIcSkipParseFlag() ? 1 : 0, "ic_skip_mergeidx0" );
2138      }
2139    }
2140#endif
2141#if H_3D_IV_MERGE
2142    assert(pcSlice->getMaxNumMergeCand()<=MRG_MAX_NUM_CANDS_MEM);
2143#else
2144    assert(pcSlice->getMaxNumMergeCand()<=MRG_MAX_NUM_CANDS);
2145#endif
2146    if (!pcSlice->isIntra())
2147    {
2148#if H_3D_IV_MERGE
2149      WRITE_UVLC( ( ( pcSlice->getMpiFlag( ) || pcSlice->getIvMvPredFlag( ) || pcSlice->getViewSynthesisPredFlag( ) ) ? MRG_MAX_NUM_CANDS_MEM : MRG_MAX_NUM_CANDS ) - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
2150#else
2151      WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
2152#endif
2153    }
2154    Int iCode = pcSlice->getSliceQp() - ( pcSlice->getPPS()->getPicInitQPMinus26() + 26 );
2155    WRITE_SVLC( iCode, "slice_qp_delta" );
2156    if (pcSlice->getPPS()->getSliceChromaQpFlag())
2157    {
2158      if (numberValidComponents > COMPONENT_Cb)
2159      {
2160        WRITE_SVLC( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb), "slice_cb_qp_offset" );
2161      }
2162      if (numberValidComponents > COMPONENT_Cr)
2163      {
2164        WRITE_SVLC( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr), "slice_cr_qp_offset" );
2165      }
2166      assert(numberValidComponents <= COMPONENT_Cr+1);
2167    }
2168
2169    if (pcSlice->getPPS()->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag())
2170    {
2171      WRITE_FLAG(pcSlice->getUseChromaQpAdj(), "cu_chroma_qp_offset_enabled_flag");
2172    }
2173
2174    if (pcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
2175    {
2176      if (pcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag() )
2177      {
2178        WRITE_FLAG(pcSlice->getDeblockingFilterOverrideFlag(), "deblocking_filter_override_flag");
2179      }
2180      if (pcSlice->getDeblockingFilterOverrideFlag())
2181      {
2182        WRITE_FLAG(pcSlice->getDeblockingFilterDisable(), "slice_disable_deblocking_filter_flag");
2183        if(!pcSlice->getDeblockingFilterDisable())
2184        {
2185          WRITE_SVLC (pcSlice->getDeblockingFilterBetaOffsetDiv2(), "slice_beta_offset_div2");
2186          WRITE_SVLC (pcSlice->getDeblockingFilterTcOffsetDiv2(),   "slice_tc_offset_div2");
2187        }
2188      }
2189    }
2190
2191    Bool isSAOEnabled = pcSlice->getSPS()->getUseSAO() && (pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA) || (chromaEnabled && pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA)));
2192    Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
2193
2194    if(pcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
2195    {
2196      WRITE_FLAG(pcSlice->getLFCrossSliceBoundaryFlag()?1:0, "slice_loop_filter_across_slices_enabled_flag");
2197    }
2198#if NH_3D
2199    if (getEncTop()->decProcAnnexI() )
2200    {
2201      Int voiInVps = vps->getVoiInVps( pcSlice->getViewIndex() ); 
2202      if( vps->getCpInSliceSegmentHeaderFlag( voiInVps ) && !pcSlice->getIsDepth() )
2203      {
2204        for( Int m = 0; m < vps->getNumCp( voiInVps ); m++ )
2205        {
2206          Int jInVps = vps->getVoiInVps( vps->getCpRefVoi( voiInVps, m ));
2207          WRITE_SVLC( pcSlice->getCpScale   ( jInVps )   , "cp_scale" );
2208          WRITE_SVLC( pcSlice->getCpOff     ( jInVps )   , "cp_off" );
2209          WRITE_SVLC( pcSlice->getCpInvScale( jInVps ) + pcSlice->getCpScale( jInVps ) , "cp_inv_scale_plus_scale" );
2210          WRITE_SVLC( pcSlice->getCpInvOff  ( jInVps ) + pcSlice->getCpOff  ( jInVps ) , "cp_inv_off_plus_off" );
2211        }
2212      }
2213    }
2214#endif
2215
2216  }
2217#if !NH_MV
2218  if(pcSlice->getPPS()->getSliceHeaderExtensionPresentFlag())
2219  {
2220    WRITE_UVLC(0,"slice_segment_header_extension_length");
2221  }
2222#else
2223  if(pcSlice->getPPS()->getSliceHeaderExtensionPresentFlag())
2224  {
2225    // Derive the value of PocMsbValRequiredFlag
2226
2227    // Determine value of SH extension length.
2228    Int shExtnLengthInBit = 0;
2229    if (pcSlice->getPPS()->getPocResetInfoPresentFlag())
2230    {
2231      shExtnLengthInBit += 2;
2232    }
2233    if (pcSlice->getPocResetIdc() > 0)
2234    {
2235      shExtnLengthInBit += 6;
2236    }
2237    if (pcSlice->getPocResetIdc() == 3)
2238    {
2239      shExtnLengthInBit += (pcSlice->getSPS()->getBitsForPOC() + 1);
2240    }
2241
2242
2243    if( !pcSlice->getPocMsbValRequiredFlag() &&  pcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
2244    {
2245      shExtnLengthInBit++;    // For poc_msb_val_present_flag
2246    }
2247    else
2248    {
2249      if( pcSlice->getPocMsbValRequiredFlag() )
2250      {
2251        pcSlice->setPocMsbValPresentFlag( true );
2252      }
2253      else
2254      {
2255        pcSlice->setPocMsbValPresentFlag( false );
2256      }
2257    }
2258
2259    if( pcSlice->getPocMsbValPresentFlag() )
2260    {
2261//      Int iMaxPOClsb = 1<< pcSlice->getSPS()->getBitsForPOC(); currently unused
2262
2263      UInt lengthVal = 1;
2264      UInt tempVal = pcSlice->getPocMsbVal() + 1;
2265      assert ( tempVal );
2266      while( 1 != tempVal )
2267      {
2268        tempVal >>= 1;
2269        lengthVal += 2;
2270      }
2271      shExtnLengthInBit += lengthVal;
2272    }
2273    Int shExtnAdditionalBits = 0;
2274    if(shExtnLengthInBit % 8 != 0)
2275    {
2276      shExtnAdditionalBits = 8 - (shExtnLengthInBit % 8);
2277    }
2278    pcSlice->setSliceSegmentHeaderExtensionLength((shExtnLengthInBit + shExtnAdditionalBits) / 8);
2279     
2280
2281    WRITE_UVLC( pcSlice->getSliceSegmentHeaderExtensionLength( ), "slice_segment_header_extension_length" );
2282    UInt posFollSliceSegHeaderExtLen = m_pcBitIf->getNumberOfWrittenBits();
2283    if( pcSlice->getPPS()->getPocResetInfoPresentFlag() )
2284    {
2285      WRITE_CODE( pcSlice->getPocResetIdc( ), 2, "poc_reset_idc" );
2286    }
2287    else
2288    {
2289      assert( pcSlice->getPocResetIdc( ) == 0 );
2290    }
2291
2292    pcSlice->checkPocResetIdc(); 
2293
2294    if( pcSlice->getPocResetIdc() !=  0 )
2295    {
2296      WRITE_CODE( pcSlice->getPocResetPeriodId( ), 6, "poc_reset_period_id" );
2297    }
2298   
2299    if( pcSlice->getPocResetIdc() ==  3 ) 
2300    {
2301      WRITE_FLAG( pcSlice->getFullPocResetFlag( ) ? 1 : 0 , "full_poc_reset_flag" );
2302      WRITE_CODE( pcSlice->getPocLsbVal( ), pcSlice->getPocLsbValLen() , "poc_lsb_val" );
2303    }             
2304    pcSlice->checkPocLsbVal(); 
2305
2306    if( !pcSlice->getPocMsbValRequiredFlag() &&  pcSlice->getVPS()->getVpsPocLsbAlignedFlag()  )
2307    {
2308      WRITE_FLAG( pcSlice->getPocMsbValPresentFlag( ) ? 1 : 0 , "poc_msb_val_present_flag" );
2309    }
2310    else
2311    {
2312      assert( pcSlice->getPocMsbValPresentFlag() ==  pcSlice->inferPocMsbValPresentFlag( ) ); 
2313    }
2314   
2315    if( pcSlice->getPocMsbValPresentFlag() )
2316    {
2317      WRITE_UVLC( pcSlice->getPocMsbVal( ), "poc_msb_val" );
2318    }
2319   
2320    while( ( m_pcBitIf->getNumberOfWrittenBits() - posFollSliceSegHeaderExtLen ) < pcSlice->getSliceSegmentHeaderExtensionLength() * 8 )
2321    {
2322      WRITE_FLAG( 0, "slice_segment_header_extension_data_bit" );
2323    }
2324
2325    assert( ( m_pcBitIf->getNumberOfWrittenBits() - posFollSliceSegHeaderExtLen ) == pcSlice->getSliceSegmentHeaderExtensionLength() * 8  ); 
2326  }
2327#endif
2328
2329}
2330
2331Void TEncCavlc::codePTL( const TComPTL* pcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1)
2332{
2333  if(profilePresentFlag)
2334  {
2335    codeProfileTier(pcPTL->getGeneralPTL(), false);    // general_...
2336  }
2337  WRITE_CODE( Int(pcPTL->getGeneralPTL()->getLevelIdc()), 8, "general_level_idc" );
2338
2339  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
2340  {
2341#if !NH_MV
2342#endif
2343    WRITE_FLAG( pcPTL->getSubLayerProfilePresentFlag(i), "sub_layer_profile_present_flag[i]" );
2344#if !NH_MV
2345#endif
2346    WRITE_FLAG( pcPTL->getSubLayerLevelPresentFlag(i),   "sub_layer_level_present_flag[i]" );
2347  }
2348
2349  if (maxNumSubLayersMinus1 > 0)
2350  {
2351    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
2352    {
2353      WRITE_CODE(0, 2, "reserved_zero_2bits");
2354    }
2355  }
2356
2357  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
2358  {
2359    if( pcPTL->getSubLayerProfilePresentFlag(i) )
2360    {
2361      codeProfileTier(pcPTL->getSubLayerPTL(i), true);  // sub_layer_...
2362    }
2363    if( pcPTL->getSubLayerLevelPresentFlag(i) )
2364    {
2365      WRITE_CODE( Int(pcPTL->getSubLayerPTL(i)->getLevelIdc()), 8, "sub_layer_level_idc[i]" );
2366    }
2367  }
2368}
2369
2370#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
2371Void TEncCavlc::codeProfileTier( const ProfileTierLevel* ptl, const Bool bIsSubLayer )
2372#define PTL_TRACE_TEXT(txt) bIsSubLayer?("sub_layer_" txt) : ("general_" txt)
2373#else
2374Void TEncCavlc::codeProfileTier( const ProfileTierLevel* ptl, const Bool /*bIsSubLayer*/ )
2375#define PTL_TRACE_TEXT(txt) txt
2376#endif
2377{
2378  WRITE_CODE( ptl->getProfileSpace(), 2 ,      PTL_TRACE_TEXT("profile_space"                   ));
2379  WRITE_FLAG( ptl->getTierFlag()==Level::HIGH, PTL_TRACE_TEXT("tier_flag"                       ));
2380  WRITE_CODE( Int(ptl->getProfileIdc()), 5 ,   PTL_TRACE_TEXT("profile_idc"                     ));
2381  for(Int j = 0; j < 32; j++)
2382  {
2383    WRITE_FLAG( ptl->getProfileCompatibilityFlag(j), PTL_TRACE_TEXT("profile_compatibility_flag[][j]" ));
2384  }
2385
2386  WRITE_FLAG(ptl->getProgressiveSourceFlag(),   PTL_TRACE_TEXT("progressive_source_flag"         ));
2387  WRITE_FLAG(ptl->getInterlacedSourceFlag(),    PTL_TRACE_TEXT("interlaced_source_flag"          ));
2388  WRITE_FLAG(ptl->getNonPackedConstraintFlag(), PTL_TRACE_TEXT("non_packed_constraint_flag"      ));
2389  WRITE_FLAG(ptl->getFrameOnlyConstraintFlag(), PTL_TRACE_TEXT("frame_only_constraint_flag"      ));
2390
2391#if NH_MV
2392  if( ptl->getV2ConstraintsPresentFlag() ) 
2393  {
2394    WRITE_FLAG( ptl->getMax12bitConstraintFlag( ) ? 1 : 0 , "max_12bit_constraint_flag" );
2395    WRITE_FLAG( ptl->getMax10bitConstraintFlag( ) ? 1 : 0 , "max_10bit_constraint_flag" );
2396    WRITE_FLAG( ptl->getMax8bitConstraintFlag( ) ? 1 : 0 , "max_8bit_constraint_flag" );
2397    WRITE_FLAG( ptl->getMax422chromaConstraintFlag( ) ? 1 : 0 , "max_422chroma_constraint_flag" );
2398    WRITE_FLAG( ptl->getMax420chromaConstraintFlag( ) ? 1 : 0 , "max_420chroma_constraint_flag" );
2399    WRITE_FLAG( ptl->getMaxMonochromeConstraintFlag( ) ? 1 : 0 , "max_monochrome_constraint_flag" );
2400    WRITE_FLAG( ptl->getIntraConstraintFlag( ) ? 1 : 0 , "intra_constraint_flag" );
2401    WRITE_FLAG( ptl->getOnePictureOnlyConstraintFlag( ) ? 1 : 0 , "one_picture_only_constraint_flag" );
2402    WRITE_FLAG( ptl->getLowerBitRateConstraintFlag( ) ? 1 : 0 , "lower_bit_rate_constraint_flag" );
2403    WRITE_CODE( 0, 16, "XXX_reserved_zero_34bits[0..15]");
2404    WRITE_CODE( 0, 16, "XXX_reserved_zero_34bits[16..31]");
2405    WRITE_CODE( 0, 2 , "XXX_reserved_zero_34bits[32..33]");
2406  }
2407  else
2408  {
2409    WRITE_CODE( 0, 16, "XXX_reserved_zero_43bits[0..15]");
2410    WRITE_CODE( 0, 16, "XXX_reserved_zero_43bits[16..31]");
2411    WRITE_CODE( 0, 11, "XXX_reserved_zero_43bits[32..42]");
2412  }
2413    if( ptl->getInbldPresentFlag() )
2414  {
2415    WRITE_FLAG( ptl->getInbldFlag( ) ? 1 : 0 , "inbld_flag" );
2416  }
2417  else
2418  {
2419    WRITE_FLAG(0, "reserved_zero_bit");
2420  }
2421#else
2422  if (ptl->getProfileIdc() == Profile::MAINREXT || ptl->getProfileIdc() == Profile::HIGHTHROUGHPUTREXT )
2423  {
2424    const UInt         bitDepthConstraint=ptl->getBitDepthConstraint();
2425    WRITE_FLAG(bitDepthConstraint<=12,          PTL_TRACE_TEXT("max_12bit_constraint_flag"       ));
2426    WRITE_FLAG(bitDepthConstraint<=10,          PTL_TRACE_TEXT("max_10bit_constraint_flag"       ));
2427    WRITE_FLAG(bitDepthConstraint<= 8,          PTL_TRACE_TEXT("max_8bit_constraint_flag"        ));
2428    const ChromaFormat chromaFmtConstraint=ptl->getChromaFormatConstraint();
2429    WRITE_FLAG(chromaFmtConstraint==CHROMA_422||chromaFmtConstraint==CHROMA_420||chromaFmtConstraint==CHROMA_400, PTL_TRACE_TEXT("max_422chroma_constraint_flag" ));
2430    WRITE_FLAG(chromaFmtConstraint==CHROMA_420||chromaFmtConstraint==CHROMA_400,                                  PTL_TRACE_TEXT("max_420chroma_constraint_flag" ));
2431    WRITE_FLAG(chromaFmtConstraint==CHROMA_400,                                                                   PTL_TRACE_TEXT("max_monochrome_constraint_flag"));
2432    WRITE_FLAG(ptl->getIntraConstraintFlag(),        PTL_TRACE_TEXT("intra_constraint_flag"           ));
2433    WRITE_FLAG(ptl->getOnePictureOnlyConstraintFlag(), PTL_TRACE_TEXT("one_picture_only_constraint_flag"));
2434    WRITE_FLAG(ptl->getLowerBitRateConstraintFlag(), PTL_TRACE_TEXT("lower_bit_rate_constraint_flag"  ));
2435    WRITE_CODE(0 , 16, PTL_TRACE_TEXT("reserved_zero_34bits[0..15]"     ));
2436    WRITE_CODE(0 , 16, PTL_TRACE_TEXT("reserved_zero_34bits[16..31]"    ));
2437    WRITE_CODE(0 ,  2, PTL_TRACE_TEXT("reserved_zero_34bits[32..33]"    ));
2438  }
2439  else
2440  {
2441    WRITE_CODE(0x0000 , 16, PTL_TRACE_TEXT("reserved_zero_43bits[0..15]"     ));
2442    WRITE_CODE(0x0000 , 16, PTL_TRACE_TEXT("reserved_zero_43bits[16..31]"    ));
2443    WRITE_CODE(0x000  , 11, PTL_TRACE_TEXT("reserved_zero_43bits[32..42]"    ));
2444  }
2445  WRITE_FLAG(false,   PTL_TRACE_TEXT("inbld_flag" ));
2446#undef PTL_TRACE_TEXT
2447#endif
2448}
2449
2450/**
2451 * Write tiles and wavefront substreams sizes for the slice header (entry points).
2452 *
2453 * \param pSlice TComSlice structure that contains the substream size information.
2454 */
2455Void  TEncCavlc::codeTilesWPPEntryPoint( TComSlice* pSlice )
2456{
2457  if (!pSlice->getPPS()->getTilesEnabledFlag() && !pSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
2458  {
2459    return;
2460  }
2461  UInt maxOffset = 0;
2462  for(Int idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2463  {
2464    UInt offset=pSlice->getSubstreamSize(idx);
2465    if ( offset > maxOffset )
2466    {
2467      maxOffset = offset;
2468    }
2469  }
2470
2471  // Determine number of bits "offsetLenMinus1+1" required for entry point information
2472  UInt offsetLenMinus1 = 0;
2473  while (maxOffset >= (1u << (offsetLenMinus1 + 1)))
2474  {
2475    offsetLenMinus1++;
2476    assert(offsetLenMinus1 + 1 < 32);
2477  }
2478
2479  WRITE_UVLC(pSlice->getNumberOfSubstreamSizes(), "num_entry_point_offsets");
2480  if (pSlice->getNumberOfSubstreamSizes()>0)
2481  {
2482    WRITE_UVLC(offsetLenMinus1, "offset_len_minus1");
2483
2484    for (UInt idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2485    {
2486      WRITE_CODE(pSlice->getSubstreamSize(idx)-1, offsetLenMinus1+1, "entry_point_offset_minus1");
2487    }
2488  }
2489}
2490
2491Void TEncCavlc::codeTerminatingBit      ( UInt /*uilsLast*/ )
2492{
2493}
2494
2495Void TEncCavlc::codeSliceFinish ()
2496{
2497}
2498
2499Void TEncCavlc::codeMVPIdx ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, RefPicList /*eRefList*/ )
2500{
2501  assert(0);
2502}
2503
2504Void TEncCavlc::codePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2505{
2506  assert(0);
2507}
2508
2509Void TEncCavlc::codePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2510{
2511  assert(0);
2512}
2513
2514Void TEncCavlc::codeMergeFlag    ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2515{
2516  assert(0);
2517}
2518
2519Void TEncCavlc::codeMergeIndex    ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2520{
2521  assert(0);
2522}
2523
2524#if H_3D_ARP
2525Void TEncCavlc::codeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
2526{
2527  assert(0);
2528}
2529#endif
2530
2531#if H_3D_IC
2532Void TEncCavlc::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2533{
2534  assert(0);
2535}
2536#endif
2537
2538Void TEncCavlc::codeInterModeFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiEncMode*/ )
2539{
2540  assert(0);
2541}
2542
2543Void TEncCavlc::codeCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2544{
2545  assert(0);
2546}
2547
2548Void TEncCavlc::codeSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2549{
2550  assert(0);
2551}
2552#if H_3D
2553Void TEncCavlc::codeDIS( TComDataCU* pcCU, UInt uiAbsPartIdx )
2554{
2555  assert(0);
2556}
2557#endif
2558
2559Void TEncCavlc::codeSplitFlag   ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2560{
2561  assert(0);
2562}
2563
2564Void TEncCavlc::codeTransformSubdivFlag( UInt /*uiSymbol*/, UInt /*uiCtx*/ )
2565{
2566  assert(0);
2567}
2568
2569Void TEncCavlc::codeQtCbf( TComTU& /*rTu*/, const ComponentID /*compID*/, const Bool /*lowestLevel*/ )
2570{
2571  assert(0);
2572}
2573
2574Void TEncCavlc::codeQtRootCbf( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2575{
2576  assert(0);
2577}
2578
2579Void TEncCavlc::codeQtCbfZero( TComTU& /*rTu*/, const ChannelType /*chType*/ )
2580{
2581  assert(0);
2582}
2583Void TEncCavlc::codeQtRootCbfZero( )
2584{
2585  assert(0);
2586}
2587
2588Void TEncCavlc::codeTransformSkipFlags (TComTU& /*rTu*/, ComponentID /*component*/ )
2589{
2590  assert(0);
2591}
2592
2593/** Code I_PCM information.
2594 * \param pcCU pointer to CU
2595 * \param uiAbsPartIdx CU index
2596 * \returns Void
2597 */
2598Void TEncCavlc::codeIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2599{
2600  assert(0);
2601}
2602
2603Void TEncCavlc::codeIntraDirLumaAng( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, Bool /*isMultiple*/)
2604{
2605  assert(0);
2606}
2607
2608Void TEncCavlc::codeIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2609{
2610  assert(0);
2611}
2612
2613Void TEncCavlc::codeInterDir( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2614{
2615  assert(0);
2616}
2617
2618Void TEncCavlc::codeRefFrmIdx( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, RefPicList /*eRefList*/ )
2619{
2620  assert(0);
2621}
2622
2623Void TEncCavlc::codeMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, RefPicList /*eRefList*/ )
2624{
2625  assert(0);
2626}
2627
2628Void TEncCavlc::codeCrossComponentPrediction( TComTU& /*rTu*/, ComponentID /*compID*/ )
2629{
2630  assert(0);
2631}
2632
2633Void TEncCavlc::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
2634{
2635  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
2636
2637  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
2638  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
2639
2640  xWriteSvlc( iDQp );
2641
2642  return;
2643}
2644
2645Void TEncCavlc::codeChromaQpAdjustment( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2646{
2647  assert(0);
2648}
2649
2650Void TEncCavlc::codeCoeffNxN    ( TComTU& /*rTu*/, TCoeff* /*pcCoef*/, const ComponentID /*compID*/ )
2651{
2652  assert(0);
2653}
2654
2655Void TEncCavlc::estBit( estBitsSbacStruct* /*pcEstBitsCabac*/, Int /*width*/, Int /*height*/, ChannelType /*chType*/ )
2656{
2657  // printf("error : no VLC mode support in this version\n");
2658  return;
2659}
2660
2661// ====================================================================================================================
2662// Protected member functions
2663// ====================================================================================================================
2664
2665//! Code weighted prediction tables
2666Void TEncCavlc::xCodePredWeightTable( TComSlice* pcSlice )
2667{
2668  WPScalingParam  *wp;
2669  const ChromaFormat    format                = pcSlice->getPic()->getChromaFormat();
2670  const UInt            numberValidComponents = getNumberValidComponents(format);
2671  const Bool            bChroma               = isChromaEnabled(format);
2672  const Int             iNbRef                = (pcSlice->getSliceType() == B_SLICE ) ? (2) : (1);
2673        Bool            bDenomCoded           = false;
2674        UInt            uiTotalSignalledWeightFlags = 0;
2675
2676  if ( (pcSlice->getSliceType()==P_SLICE && pcSlice->getPPS()->getUseWP()) || (pcSlice->getSliceType()==B_SLICE && pcSlice->getPPS()->getWPBiPred()) )
2677  {
2678    for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
2679    {
2680      RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2681
2682      // NOTE: wp[].uiLog2WeightDenom and wp[].bPresentFlag are actually per-channel-type settings.
2683
2684      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2685      {
2686        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2687        if ( !bDenomCoded )
2688        {
2689          Int iDeltaDenom;
2690          WRITE_UVLC( wp[COMPONENT_Y].uiLog2WeightDenom, "luma_log2_weight_denom" );
2691
2692          if( bChroma )
2693          {
2694            assert(wp[COMPONENT_Cb].uiLog2WeightDenom == wp[COMPONENT_Cr].uiLog2WeightDenom); // check the channel-type settings are consistent across components.
2695            iDeltaDenom = (wp[COMPONENT_Cb].uiLog2WeightDenom - wp[COMPONENT_Y].uiLog2WeightDenom);
2696            WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
2697          }
2698          bDenomCoded = true;
2699        }
2700        WRITE_FLAG( wp[COMPONENT_Y].bPresentFlag, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
2701        uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
2702      }
2703      if (bChroma)
2704      {
2705        for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2706        {
2707          pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2708          assert(wp[COMPONENT_Cb].bPresentFlag == wp[COMPONENT_Cr].bPresentFlag); // check the channel-type settings are consistent across components.
2709          WRITE_FLAG( wp[COMPONENT_Cb].bPresentFlag, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
2710          uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
2711        }
2712      }
2713
2714      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2715      {
2716        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2717        if ( wp[COMPONENT_Y].bPresentFlag )
2718        {
2719          Int iDeltaWeight = (wp[COMPONENT_Y].iWeight - (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
2720          WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
2721          WRITE_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
2722        }
2723
2724        if ( bChroma )
2725        {
2726          if ( wp[COMPONENT_Cb].bPresentFlag )
2727          {
2728            for ( Int j = COMPONENT_Cb ; j < numberValidComponents ; j++ )
2729            {
2730              assert(wp[COMPONENT_Cb].uiLog2WeightDenom == wp[COMPONENT_Cr].uiLog2WeightDenom);
2731              Int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMPONENT_Cb].uiLog2WeightDenom));
2732              WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
2733
2734              Int range=pcSlice->getSPS()->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
2735              Int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
2736              Int iDeltaChroma = (wp[j].iOffset - pred);
2737              WRITE_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
2738            }
2739          }
2740        }
2741      }
2742    }
2743    assert(uiTotalSignalledWeightFlags<=24);
2744  }
2745}
2746
2747/** code quantization matrix
2748 *  \param scalingList quantization matrix information
2749 */
2750Void TEncCavlc::codeScalingList( const TComScalingList &scalingList )
2751{
2752  //for each size
2753  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2754  {
2755    const Int predListStep = (sizeId == SCALING_LIST_32x32? (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) : 1); // if 32x32, skip over chroma entries.
2756
2757    for(UInt listId = 0; listId < SCALING_LIST_NUM; listId+=predListStep)
2758    {
2759      Bool scalingListPredModeFlag = scalingList.getScalingListPredModeFlag(sizeId, listId);
2760      WRITE_FLAG( scalingListPredModeFlag, "scaling_list_pred_mode_flag" );
2761      if(!scalingListPredModeFlag)// Copy Mode
2762      {
2763        if (sizeId == SCALING_LIST_32x32)
2764        {
2765          // adjust the code, to cope with the missing chroma entries
2766          WRITE_UVLC( ((Int)listId - (Int)scalingList.getRefMatrixId (sizeId,listId)) / (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES), "scaling_list_pred_matrix_id_delta");
2767        }
2768        else
2769        {
2770          WRITE_UVLC( (Int)listId - (Int)scalingList.getRefMatrixId (sizeId,listId), "scaling_list_pred_matrix_id_delta");
2771        }
2772      }
2773      else// DPCM Mode
2774      {
2775        xCodeScalingList(&scalingList, sizeId, listId);
2776      }
2777    }
2778  }
2779  return;
2780}
2781/** code DPCM
2782 * \param scalingList quantization matrix information
2783 * \param sizeId      size index
2784 * \param listId      list index
2785 */
2786Void TEncCavlc::xCodeScalingList(const TComScalingList* scalingList, UInt sizeId, UInt listId)
2787{
2788  Int coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
2789  UInt* scan  = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId==0 ? 2 : 3][sizeId==0 ? 2 : 3];
2790  Int nextCoef = SCALING_LIST_START_VALUE;
2791  Int data;
2792  const Int *src = scalingList->getScalingListAddress(sizeId, listId);
2793    if( sizeId > SCALING_LIST_8x8 )
2794    {
2795      WRITE_SVLC( scalingList->getScalingListDC(sizeId,listId) - 8, "scaling_list_dc_coef_minus8");
2796      nextCoef = scalingList->getScalingListDC(sizeId,listId);
2797    }
2798    for(Int i=0;i<coefNum;i++)
2799    {
2800      data = src[scan[i]] - nextCoef;
2801      nextCoef = src[scan[i]];
2802      if(data > 127)
2803      {
2804        data = data - 256;
2805      }
2806      if(data < -128)
2807      {
2808        data = data + 256;
2809      }
2810
2811      WRITE_SVLC( data,  "scaling_list_delta_coef");
2812    }
2813}
2814Bool TEncCavlc::findMatchingLTRP ( TComSlice* pcSlice, UInt *ltrpsIndex, Int ltrpPOC, Bool usedFlag )
2815{
2816  // Bool state = true, state2 = false;
2817  Int lsb = ltrpPOC & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
2818  for (Int k = 0; k < pcSlice->getSPS()->getNumLongTermRefPicSPS(); k++)
2819  {
2820    if ( (lsb == pcSlice->getSPS()->getLtRefPicPocLsbSps(k)) && (usedFlag == pcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(k)) )
2821    {
2822      *ltrpsIndex = k;
2823      return true;
2824    }
2825  }
2826  return false;
2827}
2828
2829Void TEncCavlc::codeExplicitRdpcmMode( TComTU& /*rTu*/, const ComponentID /*compID*/ )
2830 {
2831   assert(0);
2832 }
2833#if NH_3D_SDC_INTRA || H_3D_INTER_SDC
2834Void TEncCavlc::codeSDCFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx )
2835{
2836  assert(0);
2837}
2838#endif
2839   
2840#if H_3D_DBBP
2841Void TEncCavlc::codeDBBPFlag    ( TComDataCU* pcCU, UInt uiAbsPartIdx )
2842{
2843  assert(0);
2844}
2845#endif
2846
2847
2848//! \}
Note: See TracBrowser for help on using the repository browser.