Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/EncoderLib/VLCWriter.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
43
44
/** \file     VLCWriter.cpp
45
 *  \brief    Writer for high level syntax
46
 */
47
48
#include "VLCWriter.h"
49
#include "SEIwrite.h"
50
#include "CommonLib/CommonDef.h"
51
#include "CommonLib/Unit.h"
52
#include "CommonLib/Picture.h" // th remove this
53
#include "CommonLib/dtrace_next.h"
54
55
//! \ingroup EncoderLib
56
//! \{
57
58
namespace vvenc {
59
60
#if ENABLE_TRACING
61
62
void  VLCWriter::xWriteSCodeTr (int value, uint32_t  length, const char *pSymbolName)
63
{
64
  xWriteSCode (value,length);
65
  if( g_HLSTraceEnable )
66
  {
67
    if( length<10 )
68
    {
69
      DTRACE( g_trace_ctx, D_HEADER, "%-50s u(%d)  : %d\n", pSymbolName, length, value );
70
    }
71
    else
72
    {
73
      DTRACE( g_trace_ctx, D_HEADER, "%-50s u(%d) : %d\n", pSymbolName, length, value );
74
    }
75
  }
76
}
77
78
void  VLCWriter::xWriteCodeTr (uint32_t value, uint32_t  length, const char *pSymbolName)
79
{
80
  xWriteCode (value,length);
81
82
  if( g_HLSTraceEnable )
83
  {
84
    if( length < 10 )
85
    {
86
      DTRACE( g_trace_ctx, D_HEADER, "%-50s u(%d)  : %d\n", pSymbolName, length, value );
87
    }
88
    else
89
    {
90
      DTRACE( g_trace_ctx, D_HEADER, "%-50s u(%d) : %d\n", pSymbolName, length, value );
91
    }
92
  }
93
}
94
95
void  VLCWriter::xWriteUvlcTr (uint32_t value, const char *pSymbolName)
96
{
97
  xWriteUvlc (value);
98
  if( g_HLSTraceEnable )
99
  {
100
    DTRACE( g_trace_ctx, D_HEADER, "%-50s ue(v) : %d\n", pSymbolName, value );
101
  }
102
}
103
104
void  VLCWriter::xWriteSvlcTr (int value, const char *pSymbolName)
105
{
106
  xWriteSvlc(value);
107
  if( g_HLSTraceEnable )
108
  {
109
    DTRACE( g_trace_ctx, D_HEADER, "%-50s se(v) : %d\n", pSymbolName, value );
110
  }
111
}
112
113
void  VLCWriter::xWriteFlagTr(bool flag, const char *pSymbolName)
114
{
115
  xWriteFlag(flag);
116
  if( g_HLSTraceEnable )
117
  {
118
    DTRACE( g_trace_ctx, D_HEADER, "%-50s u(1)  : %d\n", pSymbolName, flag?1:0 );
119
  }
120
}
121
122
bool g_HLSTraceEnable = true;
123
124
#endif
125
126
void VLCWriter::xWriteSCode    ( int code, uint32_t length )
127
0
{
128
0
  assert ( length > 0 && length<=32 );
129
0
  assert( length==32 || (code>=-(1<<(length-1)) && code<(1<<(length-1))) );
130
0
  m_pcBitIf->write( length==32 ? uint32_t(code) : ( uint32_t(code)&((1<<length)-1) ), length );
131
0
}
132
133
void VLCWriter::xWriteCode     ( uint32_t uiCode, uint32_t uiLength )
134
22.9k
{
135
22.9k
  CHECK( uiLength == 0, "Code of length '0' not supported" );
136
22.9k
  m_pcBitIf->write( uiCode, uiLength );
137
22.9k
}
138
139
void VLCWriter::xWriteUvlc     ( uint32_t uiCode )
140
309k
{
141
309k
  uint32_t uiLength = 1;
142
309k
  uint32_t uiTemp = ++uiCode;
143
144
309k
  CHECK( !uiTemp, "Integer overflow" );
145
146
831k
  while( 1 != uiTemp )
147
521k
  {
148
521k
    uiTemp >>= 1;
149
521k
    uiLength += 2;
150
521k
  }
151
  // Take care of cases where uiLength > 32
152
309k
  m_pcBitIf->write( 0, uiLength >> 1);
153
309k
  m_pcBitIf->write( uiCode, (uiLength+1) >> 1);
154
309k
}
155
156
void VLCWriter::xWriteSvlc     ( int iCode )
157
12.8k
{
158
12.8k
  uint32_t uiCode = uint32_t( iCode <= 0 ? (-iCode)<<1 : (iCode<<1)-1);
159
12.8k
  xWriteUvlc( uiCode );
160
12.8k
}
161
162
void VLCWriter::xWriteFlag( bool flag )
163
352k
{
164
352k
  m_pcBitIf->write( flag?1:0, 1 );
165
352k
}
166
167
void VLCWriter::xWriteRbspTrailingBits()
168
2.86k
{
169
2.86k
  WRITE_FLAG( 1, "rbsp_stop_one_bit");
170
2.86k
  int cnt = 0;
171
14.6k
  while (m_pcBitIf->getNumBitsUntilByteAligned())
172
11.8k
  {
173
11.8k
    WRITE_FLAG( 0, "rbsp_alignment_zero_bit");
174
11.8k
    cnt++;
175
11.8k
  }
176
2.86k
  CHECK(cnt>=8, "More than '8' alignment bytes read");
177
2.86k
}
178
179
void HLSWriter::codeAUD(const int audIrapOrGdrAuFlag, const int pictureType)
180
0
{
181
0
  DTRACE( g_trace_ctx, D_HEADER, "=========== Access Unit Delimiter ===========\n" );
182
183
0
  CHECK(pictureType >= 3, "Invalid picture type");
184
0
  WRITE_FLAG(audIrapOrGdrAuFlag, "aud_irap_or_gdr_au_flag");
185
0
  WRITE_CODE(pictureType, 3, "pic_type");
186
0
  xWriteRbspTrailingBits();
187
0
}
188
189
void HLSWriter::xCodeRefPicList( const ReferencePictureList* rpl, bool isLongTermPresent, uint32_t ltLsbBitsCount, const bool isForbiddenZeroDeltaPoc, int rplIdx )
190
68.7k
{
191
68.7k
  uint32_t numRefPic = rpl->numberOfShorttermPictures + rpl->numberOfLongtermPictures + rpl->numberOfInterLayerPictures;
192
68.7k
  WRITE_UVLC(numRefPic, "num_ref_entries[ listIdx ][ rplsIdx ]");
193
194
68.7k
  if (isLongTermPresent && numRefPic > 0 && rplIdx != -1)
195
0
  {
196
0
    WRITE_FLAG(rpl->ltrpInSliceHeader, "ltrp_in_slice_header_flag[ listIdx ][ rplsIdx ]");
197
0
  }
198
68.7k
  int prevDelta = MAX_INT;
199
68.7k
  int deltaValue = 0;
200
68.7k
  bool firstSTRP = true;
201
236k
  for (int ii = 0; ii < numRefPic; ii++)
202
167k
  {
203
167k
    if( rpl->interLayerPresent )
204
0
    {
205
0
      WRITE_FLAG( rpl->isInterLayerRefPic[ii], "inter_layer_ref_pic_flag[ listIdx ][ rplsIdx ][ i ]" );
206
207
0
      if( rpl->isInterLayerRefPic[ii] )
208
0
      {
209
0
        CHECK( rpl->interLayerRefPicIdx[ii] < 0, "Wrong inter-layer reference index" );
210
0
        WRITE_UVLC( rpl->interLayerRefPicIdx[ii], "ilrp_idx[ listIdx ][ rplsIdx ][ i ]" );
211
0
      }
212
0
    }
213
214
167k
    if( !rpl->isInterLayerRefPic[ii] )
215
167k
    {
216
167k
      if (isLongTermPresent)
217
0
      {
218
0
        WRITE_FLAG(!rpl->isLongtermRefPic[ii], "st_ref_pic_flag[ listIdx ][ rplsIdx ][ i ]");
219
0
      }
220
167k
      if (!rpl->isLongtermRefPic[ii])
221
167k
      {
222
167k
        if (firstSTRP)
223
68.7k
        {
224
68.7k
          firstSTRP = false;
225
68.7k
          deltaValue = prevDelta = rpl->refPicIdentifier[ii];
226
68.7k
        }
227
98.8k
        else
228
98.8k
        {
229
98.8k
          deltaValue = rpl->refPicIdentifier[ii] - prevDelta;
230
98.8k
          prevDelta = rpl->refPicIdentifier[ii];
231
98.8k
        }
232
167k
        unsigned int absDeltaValue = (deltaValue < 0) ? 0 - deltaValue : deltaValue;
233
167k
        if( isForbiddenZeroDeltaPoc || ii == 0 )
234
167k
        {
235
167k
          CHECK( !absDeltaValue, "Zero delta POC is not used without WP" );
236
167k
          WRITE_UVLC( absDeltaValue - 1, "abs_delta_poc_st[ listIdx ][ rplsIdx ][ i ]" );
237
167k
        }
238
0
        else
239
0
        WRITE_UVLC(absDeltaValue, "abs_delta_poc_st[ listIdx ][ rplsIdx ][ i ]");
240
167k
        if (absDeltaValue > 0)
241
167k
        {
242
167k
          WRITE_FLAG((deltaValue < 0), "strp_entry_sign_flag[ listIdx ][ rplsIdx ][ i ]");  //0  means negative delta POC : 1 means positive
243
167k
        }
244
167k
      }
245
0
      else if (!rpl->ltrpInSliceHeader)
246
0
      {
247
0
        WRITE_CODE(rpl->refPicIdentifier[ii], ltLsbBitsCount, "poc_lsb_lt[listIdx][rplsIdx][i]");
248
0
      }
249
167k
    }
250
167k
  }
251
68.7k
}
252
253
void HLSWriter::codePPS( const PPS* pcPPS, const SPS* pcSPS )
254
1.43k
{
255
1.43k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Picture Parameter Set  ===========\n" );
256
257
1.43k
  WRITE_CODE( pcPPS->ppsId, 6,                        "pps_pic_parameter_set_id" );
258
1.43k
  WRITE_CODE( pcPPS->spsId, 4,                        "pps_seq_parameter_set_id" );
259
260
1.43k
  WRITE_FLAG( pcPPS->mixedNaluTypesInPic,             "pps_mixed_nalu_types_in_pic_flag" );
261
262
1.43k
  WRITE_UVLC( pcPPS->picWidthInLumaSamples,           "pic_width_in_luma_samples" );
263
1.43k
  WRITE_UVLC( pcPPS->picHeightInLumaSamples,          "pic_height_in_luma_samples" );
264
265
1.43k
  if( pcPPS->picWidthInLumaSamples == pcSPS->maxPicWidthInLumaSamples && pcPPS->picHeightInLumaSamples == pcSPS->maxPicHeightInLumaSamples )
266
1.43k
  {
267
1.43k
    WRITE_FLAG( 0,                                    "pps_conformance_window_flag" );
268
1.43k
  }
269
0
  else
270
0
  {
271
0
    const Window& conf = pcPPS->conformanceWindow;
272
0
    WRITE_FLAG( conf.enabledFlag,                     "pps_conformance_window_flag" );
273
0
    if( conf.enabledFlag )
274
0
    {
275
0
      WRITE_UVLC( conf.winLeftOffset   / SPS::getWinUnitX(pcSPS->chromaFormatIdc ), "conf_win_left_offset" );
276
0
      WRITE_UVLC( conf.winRightOffset  / SPS::getWinUnitX(pcSPS->chromaFormatIdc ), "conf_win_right_offset" );
277
0
      WRITE_UVLC( conf.winTopOffset    / SPS::getWinUnitY(pcSPS->chromaFormatIdc ), "conf_win_top_offset" );
278
0
      WRITE_UVLC( conf.winBottomOffset / SPS::getWinUnitY(pcSPS->chromaFormatIdc ), "conf_win_bottom_offset" );
279
0
    }
280
0
  }
281
282
1.43k
  const Window& scWnd = pcPPS->scalingWindow;
283
1.43k
  WRITE_FLAG( scWnd.enabledFlag,                      "pps_scaling_window_flag" );
284
1.43k
  if( scWnd.enabledFlag )
285
0
  {
286
0
    WRITE_UVLC( scWnd.winLeftOffset   / SPS::getWinUnitX(pcSPS->chromaFormatIdc ), "pps_scaling_win_left_offset" );
287
0
    WRITE_UVLC( scWnd.winRightOffset  / SPS::getWinUnitX(pcSPS->chromaFormatIdc ), "pps_scaling_win_right_offset" );
288
0
    WRITE_UVLC( scWnd.winTopOffset    / SPS::getWinUnitY(pcSPS->chromaFormatIdc ), "pps_scaling_win_top_offset" );
289
0
    WRITE_UVLC( scWnd.winBottomOffset / SPS::getWinUnitY(pcSPS->chromaFormatIdc ), "pps_scaling_win_bottom_offset" );
290
0
  }
291
292
1.43k
  WRITE_FLAG( pcPPS->outputFlagPresent,               "pps_output_flag_present_flag" );
293
1.43k
  WRITE_FLAG( pcPPS->noPicPartition,                  "pps_no_pic_partition_flag" );
294
1.43k
  WRITE_FLAG( pcPPS->subPicIdMappingInPps,            "pps_subpic_id_mapping_in_pps_flag" );
295
1.43k
  if( pcPPS->subPicIdMappingInPps )
296
0
  {
297
0
    if( pcPPS->noPicPartition )
298
0
    {
299
0
      WRITE_UVLC( pcPPS->numSubPics - 1,              "pps_num_subpics_minus1" );
300
0
    }
301
0
    WRITE_UVLC( pcPPS->subPicIdLen - 1,               "pps_subpic_id_len_minus1" );
302
303
0
    CHECK((1 << pcPPS->subPicIdLen) < pcPPS->numSubPics, "pps_subpic_id_len exceeds valid range");
304
0
    for( int picIdx = 0; picIdx < pcPPS->numSubPics; picIdx++ )
305
0
    {
306
0
      WRITE_CODE( pcPPS->subPicId[picIdx], pcPPS->subPicIdLen, "pps_subpic_id[i]" );
307
0
    }
308
0
  }
309
310
1.43k
  if( !pcPPS->noPicPartition )
311
0
  {
312
0
    WRITE_CODE( pcPPS->log2CtuSize - 5, 2, "pps_log2_ctu_size_minus5" );
313
0
    WRITE_UVLC( pcPPS->numExpTileCols - 1, "pps_num_exp_tile_columns_minus1" );
314
0
    WRITE_UVLC( pcPPS->numExpTileRows - 1, "pps_num_exp_tile_rows_minus1" );
315
316
0
    for( int colIdx = 0; colIdx < pcPPS->numExpTileCols; colIdx++ )
317
0
    {
318
0
      WRITE_UVLC( pcPPS->tileColWidth[ colIdx ] - 1,    "pps_tile_column_width_minus1[i]" );
319
0
    }
320
0
    for( int rowIdx = 0; rowIdx < pcPPS->numExpTileRows; rowIdx++ )
321
0
    {
322
0
      WRITE_UVLC( pcPPS->tileRowHeight[ rowIdx ] - 1,   "pps_tile_row_height_minus1[i]" );
323
0
    }
324
325
0
    if( pcPPS->numTileCols * pcPPS->numTileRows > 1 )
326
0
    {
327
0
      WRITE_FLAG( pcPPS->loopFilterAcrossTilesEnabled,  "pps_loop_filter_across_tiles_enabled_flag" );
328
0
      WRITE_FLAG( pcPPS->rectSlice ? 1 : 0,             "pps_rect_slice_flag" );
329
0
    }
330
0
    if( pcPPS->rectSlice )
331
0
    {
332
0
      WRITE_FLAG( pcPPS->singleSlicePerSubPic ? 1 : 0,  "pps_single_slice_per_subpic_flag" );
333
0
    }
334
0
    if( pcPPS->rectSlice & !pcPPS->singleSlicePerSubPic )
335
0
    {
336
0
      CHECK( pcPPS->numSlicesInPic > 1, "currently only one slice supported" );
337
0
      WRITE_UVLC( pcPPS->numSlicesInPic - 1,            "pps_num_slices_in_pic_minus1" );
338
0
    }
339
340
0
    if( pcPPS->rectSlice == 0 || pcPPS->singleSlicePerSubPic || pcPPS->numSlicesInPic > 1 )
341
0
    {
342
0
      WRITE_FLAG( pcPPS->loopFilterAcrossSlicesEnabled, "pps_loop_filter_across_slices_enabled_flag" );
343
0
    }
344
0
  }
345
346
1.43k
  WRITE_FLAG( pcPPS->cabacInitPresent,                "pps_cabac_init_present_flag" );
347
1.43k
  WRITE_UVLC( pcPPS->numRefIdxL0DefaultActive-1,      "pps_num_ref_idx_l0_default_active_minus1");
348
1.43k
  WRITE_UVLC( pcPPS->numRefIdxL1DefaultActive-1,      "pps_num_ref_idx_l1_default_active_minus1");
349
1.43k
  WRITE_FLAG( pcPPS->rpl1IdxPresent,                  "pps_rpl1_idx_present_flag");
350
351
1.43k
  WRITE_FLAG( pcPPS->weightPred,                      "pps_weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
352
1.43k
  WRITE_FLAG( pcPPS->weightedBiPred,                  "pps_weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
353
1.43k
  WRITE_FLAG( pcPPS->wrapAroundEnabled,               "pps_ref_wraparound_enabled_flag" );
354
1.43k
  if( pcPPS->wrapAroundEnabled )
355
0
  {
356
0
    WRITE_UVLC(pcPPS->picWidthMinusWrapAroundOffset,  "pps_pic_width_minus_wraparound_offset");
357
0
  }
358
359
1.43k
  WRITE_SVLC( pcPPS->picInitQPMinus26,                "pps_init_qp_minus26");
360
1.43k
  WRITE_FLAG( pcPPS->useDQP,                          "pps_cu_qp_delta_enabled_flag" );
361
1.43k
  WRITE_FLAG (pcPPS->usePPSChromaTool,                "pps_chroma_tool_offsets_present_flag");
362
1.43k
  if (pcPPS->usePPSChromaTool)
363
1.43k
  {
364
1.43k
    WRITE_SVLC( pcPPS->chromaQpOffset[COMP_Cb],       "pps_cb_qp_offset" );
365
1.43k
    WRITE_SVLC( pcPPS->chromaQpOffset[COMP_Cr],       "pps_cr_qp_offset" );
366
1.43k
    WRITE_FLAG( pcPPS->jointCbCrQpOffsetPresent,      "pps_joint_cbcr_qp_offset_present_flag");
367
1.43k
    if (pcPPS->jointCbCrQpOffsetPresent)
368
1.43k
    {
369
1.43k
      WRITE_SVLC(pcPPS->chromaQpOffset[COMP_JOINT_CbCr],"pps_joint_cbcr_qp_offset_value");
370
1.43k
    }
371
372
1.43k
    WRITE_FLAG( pcPPS->sliceChromaQpFlag,               "pps_slice_chroma_qp_offsets_present_flag" );
373
374
1.43k
    bool cuChromaQpOffsetEnabled = pcPPS->chromaQpOffsetListLen>0;
375
1.43k
    WRITE_FLAG(cuChromaQpOffsetEnabled,                 "pps_cu_chroma_qp_offset_list_enabled_flag" );
376
1.43k
    if( cuChromaQpOffsetEnabled )
377
0
    {
378
0
      WRITE_UVLC(pcPPS->chromaQpOffsetListLen - 1,      "pps_chroma_qp_offset_list_len_minus1");
379
      /* skip zero index */
380
0
      for (int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx < pcPPS->chromaQpOffsetListLen; cuChromaQpOffsetIdx++)
381
0
      {
382
0
        WRITE_SVLC(pcPPS->getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CbOffset,     "pps_cb_qp_offset_list[i]");
383
0
        WRITE_SVLC(pcPPS->getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CrOffset,     "pps_cr_qp_offset_list[i]");
384
0
        if (pcPPS->jointCbCrQpOffsetPresent)
385
0
        {
386
0
          WRITE_SVLC(pcPPS->getChromaQpOffsetListEntry(cuChromaQpOffsetIdx + 1).u.comp.JointCbCrOffset, "pps_joint_cbcr_qp_offset_list[i]");
387
0
        }
388
0
      }
389
0
    }
390
1.43k
  }
391
1.43k
  WRITE_FLAG( pcPPS->deblockingFilterControlPresent,    "pps_deblocking_filter_control_present_flag");
392
1.43k
  if(pcPPS->deblockingFilterControlPresent)
393
0
  {
394
0
    WRITE_FLAG( pcPPS->deblockingFilterOverrideEnabled, "pps_deblocking_filter_override_enabled_flag" );
395
0
    WRITE_FLAG( pcPPS->deblockingFilterDisabled,        "pps_deblocking_filter_disabled_flag" );
396
0
    if (!pcPPS->noPicPartition && pcPPS->deblockingFilterOverrideEnabled)
397
0
    {
398
0
      WRITE_FLAG(pcPPS->dbfInfoInPh,                    "pps_dbf_info_in_ph_flag");
399
0
    }
400
401
0
    if(!pcPPS->deblockingFilterDisabled )
402
0
    {
403
0
      WRITE_SVLC( pcPPS->deblockingFilterBetaOffsetDiv2[COMP_Y],            "pps_beta_offset_div2" );
404
0
      WRITE_SVLC( pcPPS->deblockingFilterTcOffsetDiv2[COMP_Y],              "pps_tc_offset_div2" );
405
0
      if( pcPPS->usePPSChromaTool )
406
0
      {
407
0
        WRITE_SVLC( pcPPS->deblockingFilterBetaOffsetDiv2[COMP_Cb],         "pps_cb_beta_offset_div2" );
408
0
        WRITE_SVLC( pcPPS->deblockingFilterTcOffsetDiv2[COMP_Cb],           "pps_cb_tc_offset_div2" );
409
0
        WRITE_SVLC( pcPPS->deblockingFilterBetaOffsetDiv2[COMP_Cr],         "pps_cr_beta_offset_div2" );
410
0
        WRITE_SVLC( pcPPS->deblockingFilterTcOffsetDiv2[COMP_Cr],           "pps_cr_tc_offset_div2" );
411
0
      }
412
0
    }
413
0
  }
414
1.43k
  if ( !pcPPS->noPicPartition )
415
0
  {
416
0
    WRITE_FLAG(pcPPS->rplInfoInPh,                        "pps_rpl_info_in_ph_flag");
417
0
    WRITE_FLAG(pcPPS->saoInfoInPh,                        "pps_sao_info_in_ph_flag");
418
0
    WRITE_FLAG(pcPPS->alfInfoInPh,                        "pps_alf_info_in_ph_flag");
419
0
    if( (pcPPS->weightPred || pcPPS->weightedBiPred) && pcPPS->rplInfoInPh)
420
0
    {
421
0
      WRITE_FLAG(pcPPS->wpInfoInPh,                       "pps_wp_info_in_ph_flag");
422
0
    }
423
0
    WRITE_FLAG(pcPPS->qpDeltaInfoInPh,                    "pps_qp_delta_info_in_ph_flag");
424
0
  }
425
426
1.43k
  WRITE_FLAG( pcPPS->pictureHeaderExtensionPresent,       "pps_picture_header_extension_present_flag");
427
1.43k
  WRITE_FLAG( pcPPS->sliceHeaderExtensionPresent,         "pps_slice_header_extension_present_flag");
428
429
1.43k
  WRITE_FLAG( false,                                      "pps_extension_present_flag" );
430
431
1.43k
  xWriteRbspTrailingBits();
432
1.43k
}
433
434
void HLSWriter::codeAPS( const APS* pcAPS )
435
0
{
436
0
  DTRACE(g_trace_ctx, D_HEADER, "=========== Adaptation Parameter Set  ===========\n");
437
438
0
  WRITE_CODE(pcAPS->apsType, 3,        "aps_params_type");
439
0
  WRITE_CODE(pcAPS->apsId, 5,          "adaptation_parameter_set_id");
440
0
  WRITE_FLAG(pcAPS->chromaPresent, "aps_chroma_present_flag");
441
442
0
  if (pcAPS->apsType == ALF_APS)
443
0
  {
444
0
    codeAlfAps(pcAPS);
445
0
  }
446
0
  else if( pcAPS->apsType == LMCS_APS )
447
0
  {
448
0
    THROW("no support");
449
0
  }
450
0
  else if( pcAPS->apsType == SCALING_LIST_APS )
451
0
  {
452
0
    THROW("no support");
453
0
  }
454
0
  else
455
0
  {
456
0
    THROW("invalid APS Type");
457
0
  }
458
0
  WRITE_FLAG(0, "aps_extension_flag");
459
0
  xWriteRbspTrailingBits();
460
0
}
461
462
void HLSWriter::codeAlfAps( const APS* pcAPS )
463
0
{
464
0
  const AlfParam& param = pcAPS->alfParam;
465
466
0
  WRITE_FLAG(param.newFilterFlag[CH_L],                 "alf_luma_new_filter");
467
0
  const CcAlfFilterParam& paramCcAlf = pcAPS->ccAlfParam;
468
0
  if (pcAPS->chromaPresent)
469
0
  {
470
0
    WRITE_FLAG(param.newFilterFlag[CH_C],               "alf_chroma_new_filter");
471
0
    WRITE_FLAG(paramCcAlf.newCcAlfFilter[COMP_Cb - 1],  "alf_cc_cb_filter_signal_flag");
472
0
    WRITE_FLAG(paramCcAlf.newCcAlfFilter[COMP_Cr - 1],  "alf_cc_cr_filter_signal_flag");
473
0
  }
474
475
0
  if (param.newFilterFlag[CH_L])
476
0
  {
477
0
    WRITE_FLAG( param.nonLinearFlag[CH_L],              "alf_luma_clip" );
478
479
0
    WRITE_UVLC(param.numLumaFilters - 1,                "alf_luma_num_filters_signalled_minus1");
480
0
    if (param.numLumaFilters > 1)
481
0
    {
482
0
      const int len = ceilLog2( param.numLumaFilters);
483
0
      for (int i = 0; i < MAX_NUM_ALF_CLASSES; i++)
484
0
      {
485
0
        WRITE_CODE(param.filterCoeffDeltaIdx[i], len,   "alf_luma_coeff_delta_idx" );
486
0
      }
487
0
    }
488
0
    alfFilter(param, false, 0);
489
0
  }
490
491
0
  if (param.newFilterFlag[CH_C])
492
0
  {
493
0
    WRITE_FLAG(param.nonLinearFlag[CH_C],               "alf_nonlinear_enable_flag_chroma");
494
0
    if( VVENC_MAX_NUM_ALF_ALTERNATIVES_CHROMA > 1 )
495
0
    {
496
0
      WRITE_UVLC( param.numAlternativesChroma - 1,      "alf_chroma_num_alts_minus1" );
497
0
    }
498
0
    for( int altIdx=0; altIdx < param.numAlternativesChroma; ++altIdx )
499
0
    {
500
0
      alfFilter(param, true, altIdx);
501
0
    }
502
0
  }
503
504
0
  for (int ccIdx = 0; ccIdx < 2; ccIdx++)
505
0
  {
506
0
    if (paramCcAlf.newCcAlfFilter[ccIdx])
507
0
    {
508
0
      const int filterCount = paramCcAlf.ccAlfFilterCount[ccIdx];
509
0
      CHECK(filterCount > MAX_NUM_CC_ALF_FILTERS, "CC ALF Filter count is too large");
510
0
      CHECK(filterCount == 0,                     "CC ALF Filter count is too small");
511
512
0
      WRITE_UVLC(filterCount - 1, ccIdx == 0 ? "alf_cc_cb_filters_signalled_minus1" : "alf_cc_cr_filters_signalled_minus1");
513
514
0
      for (int filterIdx = 0; filterIdx < filterCount; filterIdx++)
515
0
      {
516
0
        AlfFilterShape alfShape(size_CC_ALF);
517
518
0
        const short *coeff = paramCcAlf.ccAlfCoeff[ccIdx][filterIdx];
519
        // Filter coefficients
520
0
        for (int i = 0; i < alfShape.numCoeff - 1; i++)
521
0
        {
522
0
          if (coeff[i] == 0)
523
0
          {
524
0
            WRITE_CODE(0, CCALF_BITS_PER_COEFF_LEVEL, ccIdx == 0 ? "alf_cc_cb_mapped_coeff_abs" : "alf_cc_cr_mapped_coeff_abs");
525
0
          }
526
0
          else
527
0
          {
528
0
            WRITE_CODE(1 + floorLog2(abs(coeff[i])), CCALF_BITS_PER_COEFF_LEVEL, ccIdx == 0 ? "alf_cc_cb_mapped_coeff_abs" : "alf_cc_cr_mapped_coeff_abs");
529
0
            WRITE_FLAG(coeff[i] < 0 ? 1 : 0, ccIdx == 0 ? "alf_cc_cb_coeff_sign" : "alf_cc_cr_coeff_sign");
530
0
          }
531
0
        }
532
533
0
        DTRACE(g_trace_ctx, D_SYNTAX, "%s coeff filterIdx %d: ", ccIdx == 0 ? "Cb" : "Cr", filterIdx);
534
0
        for (int i = 0; i < alfShape.numCoeff; i++)
535
0
        {
536
0
          DTRACE(g_trace_ctx, D_SYNTAX, "%d ", coeff[i]);
537
0
        }
538
0
        DTRACE(g_trace_ctx, D_SYNTAX, "\n");
539
0
      }
540
0
    }
541
0
  }
542
0
}
543
544
void HLSWriter::codeVUI( const VUI *pcVUI, const SPS* pcSPS )
545
0
{
546
#if ENABLE_TRACING
547
  DTRACE( g_trace_ctx, D_HEADER, "----------- vui_parameters -----------\n");
548
#endif
549
550
0
  WRITE_FLAG(pcVUI->progressiveSourceFlag,                "vui_general_progressive_source_flag"         );
551
0
  WRITE_FLAG(pcVUI->interlacedSourceFlag,                 "vui_general_interlaced_source_flag"          );
552
0
  WRITE_FLAG(pcVUI->nonPackedFlag,                        "vui_non_packed_constraint_flag");
553
0
  WRITE_FLAG(pcVUI->nonProjectedFlag,                     "vui_non_projected_constraint_flag");
554
0
  WRITE_FLAG(pcVUI->aspectRatioInfoPresent,               "aspect_ratio_info_present_flag");
555
0
  if (pcVUI->aspectRatioInfoPresent)
556
0
  {
557
0
    WRITE_FLAG(pcVUI->aspectRatioConstantFlag,            "vui_aspect_ratio_constant_flag");   
558
0
    WRITE_CODE(pcVUI->aspectRatioIdc, 8,                  "aspect_ratio_idc" );
559
0
    if (pcVUI->aspectRatioIdc == 255)
560
0
    {
561
0
      WRITE_CODE(pcVUI->sarWidth, 16,                     "sar_width");
562
0
      WRITE_CODE(pcVUI->sarHeight, 16,                    "sar_height");
563
0
    }
564
0
  }
565
0
  WRITE_FLAG(pcVUI->overscanInfoPresent,                  "vui_overscan_info_present_flag");
566
0
  if (pcVUI->overscanInfoPresent)
567
0
  {
568
0
    WRITE_FLAG(pcVUI->overscanAppropriateFlag,            "vui_overscan_appropriate_flag");
569
0
  }
570
0
  WRITE_FLAG(pcVUI->colourDescriptionPresent,             "colour_description_present_flag");
571
0
  if (pcVUI->colourDescriptionPresent)
572
0
  {
573
0
    WRITE_CODE(pcVUI->colourPrimaries, 8,                 "colour_primaries");
574
0
    WRITE_CODE(pcVUI->transferCharacteristics, 8,         "transfer_characteristics");
575
0
    WRITE_CODE(pcVUI->matrixCoefficients, 8,              "matrix_coeffs");
576
0
    WRITE_FLAG(pcVUI->videoFullRangeFlag,                 "vui_video_full_range_flag");
577
0
  }
578
0
  WRITE_FLAG(pcVUI->chromaLocInfoPresent,                 "chroma_loc_info_present_flag");
579
0
  if (pcVUI->chromaLocInfoPresent)
580
0
  {
581
0
    if(pcVUI->progressiveSourceFlag && !pcVUI->interlacedSourceFlag)
582
0
    {
583
0
      WRITE_UVLC(pcVUI->chromaSampleLocType,              "chroma_sample_loc_type");
584
0
    }
585
0
    else
586
0
    {
587
0
      WRITE_UVLC(pcVUI->chromaSampleLocTypeTopField,      "chroma_sample_loc_type_top_field");
588
0
      WRITE_UVLC(pcVUI->chromaSampleLocTypeBottomField,   "chroma_sample_loc_type_bottom_field");
589
0
    }
590
0
  }
591
592
0
  if(!isByteAligned())
593
0
  {
594
0
    WRITE_FLAG(1,   "vui_payload_bit_equal_to_one");
595
0
    while(!isByteAligned())
596
0
    {
597
0
      WRITE_FLAG(0, "vui_payload_bit_equal_to_zero");
598
0
    }
599
0
  }
600
0
}
601
602
void HLSWriter::codeGeneralHrdparameters(const GeneralHrdParams * hrd)
603
1.43k
{
604
1.43k
  WRITE_CODE(hrd->numUnitsInTick, 32,                   "num_units_in_tick");
605
1.43k
  WRITE_CODE(hrd->timeScale, 32,                        "time_scale");
606
1.43k
  WRITE_FLAG(hrd->generalNalHrdParamsPresent,           "general_nal_hrd_parameters_present_flag");
607
1.43k
  WRITE_FLAG(hrd->generalVclHrdParamsPresent,           "general_vcl_hrd_parameters_present_flag");
608
1.43k
  if( hrd->generalNalHrdParamsPresent || hrd->generalVclHrdParamsPresent )
609
0
  {
610
0
    WRITE_FLAG(hrd->generalSamePicTimingInAllOlsFlag,     "general_same_pic_timing_in_all_ols_flag");
611
0
    WRITE_FLAG(hrd->generalDecodingUnitHrdParamsPresent,  "general_decoding_unit_hrd_params_present_flag");
612
0
    if (hrd->generalDecodingUnitHrdParamsPresent)
613
0
    {
614
0
      WRITE_CODE(hrd->tickDivisorMinus2, 8,               "tick_divisor_minus2");
615
0
    }
616
0
    WRITE_CODE(hrd->bitRateScale, 4,                      "bit_rate_scale");
617
0
    WRITE_CODE(hrd->cpbSizeScale, 4,                      "cpb_size_scale");
618
0
    if (hrd->generalDecodingUnitHrdParamsPresent)
619
0
    {
620
0
      WRITE_CODE(hrd->cpbSizeDuScale, 4,                  "cpb_size_du_scale");
621
0
    }
622
0
    WRITE_UVLC(hrd->hrdCpbCntMinus1,                      "hrd_cpb_cnt_minus1");
623
0
  }
624
1.43k
}
625
626
void HLSWriter::codeOlsHrdParameters(const GeneralHrdParams * generalHrd, const OlsHrdParams *olsHrd, const uint32_t firstSubLayer, const uint32_t maxNumSubLayersMinus1)
627
1.43k
{
628
2.86k
  for( int i = firstSubLayer; i <= maxNumSubLayersMinus1; i ++ )
629
1.43k
  {
630
1.43k
    const OlsHrdParams *hrd = &(olsHrd[i]);
631
1.43k
    WRITE_FLAG(hrd->fixedPicRateGeneralFlag,      "fixed_pic_rate_general_flag");
632
633
1.43k
    if (!hrd->fixedPicRateGeneralFlag)
634
0
    {
635
0
      WRITE_FLAG(hrd->fixedPicRateWithinCvsFlag,  "fixed_pic_rate_within_cvs_flag");
636
0
    }
637
1.43k
    if (hrd->fixedPicRateWithinCvsFlag)
638
1.43k
    {
639
1.43k
      WRITE_UVLC(hrd->elementDurationInTcMinus1,  "elemental_duration_in_tc_minus1");
640
1.43k
    }
641
0
    else if ( (generalHrd->generalNalHrdParamsPresent || generalHrd->generalVclHrdParamsPresent) &&generalHrd->hrdCpbCntMinus1 == 0)
642
0
    {
643
0
      WRITE_FLAG(hrd->lowDelayHrdFlag,            "low_delay_hrd_flag");
644
0
    }
645
646
4.29k
    for( int nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
647
2.86k
    {
648
2.86k
      if (((nalOrVcl == 0) && (generalHrd->generalNalHrdParamsPresent)) || ((nalOrVcl == 1) && (generalHrd->generalVclHrdParamsPresent)))
649
0
      {
650
0
        for (int j = 0; j <= (generalHrd->hrdCpbCntMinus1); j++)
651
0
        {
652
0
          WRITE_UVLC(hrd->bitRateValueMinus1[j][nalOrVcl], "bit_rate_value_minus1");
653
0
          WRITE_UVLC(hrd->cpbSizeValueMinus1[j][nalOrVcl], "cpb_size_value_minus1");
654
0
          if (generalHrd->generalDecodingUnitHrdParamsPresent)
655
0
          {
656
0
            WRITE_UVLC(hrd->duCpbSizeValueMinus1[j][nalOrVcl], "cpb_size_du_value_minus1");
657
0
            WRITE_UVLC(hrd->duBitRateValueMinus1[j][nalOrVcl], "bit_rate_du_value_minus1");
658
0
          }
659
0
          WRITE_FLAG(hrd->cbrFlag[j][nalOrVcl], "cbr_flag");
660
0
        }
661
0
      }
662
2.86k
    }
663
1.43k
  }
664
1.43k
}
665
666
void HLSWriter::dpb_parameters(int maxSubLayersMinus1, bool subLayerInfoFlag, const SPS *pcSPS)
667
1.43k
{
668
2.86k
  for (uint32_t i = (subLayerInfoFlag ? 0 : maxSubLayersMinus1); i <= maxSubLayersMinus1; i++)
669
1.43k
  {
670
1.43k
    WRITE_UVLC(pcSPS->maxDecPicBuffering[i] - 1,          "dpb_max_dec_pic_buffering_minus1[i]");
671
1.43k
    WRITE_UVLC(pcSPS->numReorderPics[i],                  "dpb_max_num_reorder_pics[i]");
672
1.43k
    WRITE_UVLC(pcSPS->maxLatencyIncreasePlus1[i],         "dpb_max_latency_increase_plus1[i]");
673
1.43k
  }
674
1.43k
}
675
676
void HLSWriter::codeSPS( const SPS* pcSPS )
677
1.43k
{
678
1.43k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Sequence Parameter Set  ===========\n" );
679
680
1.43k
  WRITE_CODE( pcSPS->spsId, 4,                            "sps_seq_parameter_set_id" );
681
1.43k
  WRITE_CODE( pcSPS->vpsId, 4,                            "sps_video_parameter_set_id" );
682
1.43k
  CHECK(pcSPS->maxTLayers == 0, "Maximum number of temporal sub-layers is '0'");
683
684
1.43k
  WRITE_CODE(pcSPS->maxTLayers - 1, 3,                    "sps_max_sub_layers_minus1");
685
1.43k
  WRITE_CODE( int(pcSPS->chromaFormatIdc), 2,             "sps_chroma_format_idc" );
686
1.43k
  WRITE_CODE(floorLog2(pcSPS->CTUSize) - 5, 2,            "sps_log2_ctu_size_minus5");
687
1.43k
  WRITE_FLAG(pcSPS->ptlDpbHrdParamsPresent,               "sps_ptl_dpb_hrd_params_present_flag");
688
689
1.43k
  if (pcSPS->ptlDpbHrdParamsPresent)
690
1.43k
  {
691
1.43k
    codeProfileTierLevel( &pcSPS->profileTierLevel, true, pcSPS->maxTLayers - 1 );
692
1.43k
  }
693
694
1.43k
  WRITE_FLAG(pcSPS->GDR,                                  "sps_gdr_enabled_flag");
695
1.43k
  WRITE_FLAG( pcSPS->rprEnabled,                          "sps_ref_pic_resampling_enabled_flag" );
696
1.43k
  if( pcSPS->rprEnabled )
697
0
  {
698
0
    WRITE_FLAG(pcSPS->resChangeInClvsEnabled, "sps_res_change_in_clvs_allowed_flag");
699
0
  }
700
1.43k
  WRITE_UVLC( pcSPS->maxPicWidthInLumaSamples,            "sps_pic_width_max_in_luma_samples" );
701
1.43k
  WRITE_UVLC( pcSPS->maxPicHeightInLumaSamples,           "sps_pic_height_max_in_luma_samples" );
702
703
1.43k
  const Window& conf = pcSPS->conformanceWindow;
704
1.43k
  WRITE_FLAG( conf.enabledFlag,                           "sps_conformance_window_flag" );
705
1.43k
  if (conf.enabledFlag)
706
0
  {
707
0
    WRITE_UVLC( conf.winLeftOffset   / SPS::getWinUnitX(pcSPS->chromaFormatIdc ), "sps_conf_win_left_offset" );
708
0
    WRITE_UVLC( conf.winRightOffset  / SPS::getWinUnitX(pcSPS->chromaFormatIdc ), "sps_conf_win_right_offset" );
709
0
    WRITE_UVLC( conf.winTopOffset    / SPS::getWinUnitY(pcSPS->chromaFormatIdc ), "sps_conf_win_top_offset" );
710
0
    WRITE_UVLC( conf.winBottomOffset / SPS::getWinUnitY(pcSPS->chromaFormatIdc ), "sps_conf_win_bottom_offset" );
711
0
  }
712
713
1.43k
  WRITE_FLAG(pcSPS->subPicInfoPresent,                    "sps_subpic_info_present_flag");
714
715
1.43k
  if (pcSPS->subPicInfoPresent)
716
0
  {
717
0
    THROW("no suppport");
718
0
  }
719
720
1.43k
  WRITE_UVLC( pcSPS->bitDepths[ CH_L ] - 8,               "sps_bitdepth_minus8" );
721
1.43k
  WRITE_FLAG( pcSPS->entropyCodingSyncEnabled,            "sps_entropy_coding_sync_enabled_flag" );
722
1.43k
  WRITE_FLAG( pcSPS->entryPointsPresent,                  "sps_entry_point_offsets_present_flag" );
723
1.43k
  WRITE_CODE( pcSPS->bitsForPOC-4, 4,                     "sps_log2_max_pic_order_cnt_lsb_minus4" );
724
1.43k
  WRITE_FLAG( pcSPS->pocMsbFlag,                          "sps_poc_msb_flag");
725
726
1.43k
  if (pcSPS->pocMsbFlag)
727
0
  {
728
0
    WRITE_UVLC(pcSPS->pocMsbLen - 1,                      "sps_poc_msb_len_minus1");
729
0
  }
730
731
1.43k
  WRITE_CODE(0, 2,                                        "sps_num_extra_ph_bits_bytes");
732
1.43k
  WRITE_CODE(0, 2,                                        "sps_num_extra_sh_bits_bytes");
733
734
1.43k
  if (pcSPS->ptlDpbHrdParamsPresent)
735
1.43k
  {
736
1.43k
    if (pcSPS->maxTLayers > 1)
737
1.43k
    {
738
1.43k
      WRITE_FLAG(pcSPS->subLayerDpbParams,                "sps_sublayer_dpb_params_flag");
739
1.43k
    }
740
1.43k
    dpb_parameters(pcSPS->maxTLayers - 1, pcSPS->subLayerDpbParams, pcSPS);
741
1.43k
  }
742
743
1.43k
  WRITE_UVLC(pcSPS->log2MinCodingBlockSize - 2,                           "log2_min_luma_coding_block_size_minus2");
744
1.43k
  WRITE_FLAG(pcSPS->partitionOverrideEnabled,                             "sps_partition_constraints_override_enabled_flag");
745
1.43k
  WRITE_UVLC(Log2(pcSPS->minQTSize[0]) - pcSPS->log2MinCodingBlockSize,   "sps_log2_diff_min_qt_min_cb_intra_slice_luma");
746
1.43k
  WRITE_UVLC(pcSPS->maxMTTDepth[0],                                       "sps_max_mtt_hierarchy_depth_intra_slice_luma");
747
1.43k
  if (pcSPS->maxMTTDepth[0] != 0)
748
1.43k
  {
749
1.43k
    WRITE_UVLC(Log2(pcSPS->maxBTSize[0]) - Log2(pcSPS->minQTSize[0]),     "sps_log2_diff_max_bt_min_qt_intra_slice_luma");
750
1.43k
    WRITE_UVLC(Log2(pcSPS->maxTTSize[0]) - Log2(pcSPS->minQTSize[0]),     "sps_log2_diff_max_tt_min_qt_intra_slice_luma");
751
1.43k
  }
752
1.43k
  if( pcSPS->chromaFormatIdc != CHROMA_400 )
753
1.43k
  {
754
1.43k
    WRITE_FLAG(pcSPS->dualITree,                                          "sps_qtbtt_dual_tree_intra_flag");
755
1.43k
  }
756
1.43k
  if (pcSPS->dualITree)
757
1.43k
  {
758
1.43k
    WRITE_UVLC(Log2(pcSPS->minQTSize[2]) - pcSPS->log2MinCodingBlockSize, "sps_log2_diff_min_qt_min_cb_intra_slice_chroma");
759
1.43k
    WRITE_UVLC(pcSPS->maxMTTDepth[2],                                     "sps_max_mtt_hierarchy_depth_intra_slice_chroma");
760
1.43k
    if (pcSPS->maxMTTDepth[2] != 0)
761
1.43k
    {
762
1.43k
      WRITE_UVLC(Log2(pcSPS->maxBTSize[2]) - Log2(pcSPS->minQTSize[2]),   "sps_log2_diff_max_bt_min_qt_intra_slice_chroma");
763
1.43k
      WRITE_UVLC(Log2(pcSPS->maxTTSize[2]) - Log2(pcSPS->minQTSize[2]),   "sps_log2_diff_max_tt_min_qt_intra_slice_chroma");
764
1.43k
    }
765
1.43k
  }
766
767
1.43k
  WRITE_UVLC(Log2(pcSPS->minQTSize[1]) - pcSPS->log2MinCodingBlockSize,   "sps_log2_diff_min_qt_min_cb_inter_slice");
768
1.43k
  WRITE_UVLC(pcSPS->maxMTTDepth[1],                                       "sps_max_mtt_hierarchy_depth_inter_slice");
769
1.43k
  if (pcSPS->maxMTTDepth[1] != 0)
770
1.43k
  {
771
1.43k
    WRITE_UVLC(Log2(pcSPS->maxBTSize[1]) - Log2(pcSPS->minQTSize[1]),     "sps_log2_diff_max_bt_min_qt_inter_slice");
772
1.43k
    WRITE_UVLC(Log2(pcSPS->maxTTSize[1]) - Log2(pcSPS->minQTSize[1]),     "sps_log2_diff_max_tt_min_qt_inter_slice");
773
1.43k
  }
774
775
1.43k
  if (pcSPS->CTUSize > 32)
776
1.43k
  {
777
1.43k
    WRITE_FLAG( (pcSPS->log2MaxTbSize - 5) != 0,                          "sps_max_luma_transform_size_64_flag" );
778
1.43k
  }
779
1.43k
  WRITE_FLAG(pcSPS->transformSkip,                                        "sps_transform_skip_enabled_flag");
780
1.43k
  if (pcSPS->transformSkip)
781
1.43k
  {
782
1.43k
    WRITE_UVLC(pcSPS->log2MaxTransformSkipBlockSize - 2,                  "sps_log2_transform_skip_max_size_minus2");
783
1.43k
    WRITE_FLAG(pcSPS->BDPCM,                                              "sps_bdpcm_enabled_flag");
784
1.43k
  }
785
1.43k
  WRITE_FLAG( pcSPS->MTS,                                                 "sps_mts_enabled_flag" );
786
1.43k
  if ( pcSPS->MTS )
787
1.43k
  {
788
1.43k
    WRITE_FLAG( pcSPS->MTSIntra,                                          "sps_explicit_mts_intra_enabled_flag" );
789
1.43k
    WRITE_FLAG( pcSPS->MTSInter,                                          "sps_explicit_mts_inter_enabled_flag" );
790
1.43k
  }
791
1.43k
  WRITE_FLAG( pcSPS->LFNST,                                               "sps_lfnst_enabled_flag");
792
793
1.43k
  if (pcSPS->chromaFormatIdc != CHROMA_400)
794
1.43k
  {
795
1.43k
    WRITE_FLAG(pcSPS->jointCbCr,                                          "sps_joint_cbcr_enabled_flag");
796
797
1.43k
    const ChromaQpMappingTable& chromaQpMappingTable = pcSPS->chromaQpMappingTable;
798
1.43k
    WRITE_FLAG(chromaQpMappingTable.m_sameCQPTableForAllChromaFlag,       "same_qp_table_for_chroma");
799
1.43k
    int numQpTables = chromaQpMappingTable.m_sameCQPTableForAllChromaFlag ? 1 : (pcSPS->jointCbCr ? 3 : 2);
800
1.43k
    CHECK(numQpTables != chromaQpMappingTable.m_numQpTables, " numQpTables does not match at encoder side ");
801
2.86k
    for (int i = 0; i < numQpTables; i++)
802
1.43k
    {
803
1.43k
      WRITE_SVLC(chromaQpMappingTable.m_qpTableStartMinus26[i],           "sps_qp_table_starts_minus26");
804
1.43k
      WRITE_UVLC(chromaQpMappingTable.m_numPtsInCQPTableMinus1[i],        "sps_num_points_in_qp_table_minus1");
805
806
5.72k
      for (int j = 0; j <= chromaQpMappingTable.m_numPtsInCQPTableMinus1[i]; j++)
807
4.29k
      {
808
4.29k
        WRITE_UVLC(chromaQpMappingTable.m_deltaQpInValMinus1[i][j],       "sps_delta_qp_in_val_minus1");
809
4.29k
        WRITE_UVLC(chromaQpMappingTable.m_deltaQpOutVal[i][j] ^ chromaQpMappingTable.m_deltaQpInValMinus1[i][j], "sps_delta_qp_diff_val");
810
4.29k
      }
811
1.43k
    }
812
1.43k
  }
813
814
1.43k
  WRITE_FLAG( pcSPS->saoEnabled,                          "sps_sao_enabled_flag");
815
1.43k
  WRITE_FLAG( pcSPS->alfEnabled,                          "sps_alf_enabled_flag" );
816
1.43k
  if (pcSPS->alfEnabled && pcSPS->chromaFormatIdc != CHROMA_400)
817
1.43k
  {
818
1.43k
    WRITE_FLAG( pcSPS->ccalfEnabled,                      "sps_ccalf_enabled_flag" );
819
1.43k
  }
820
1.43k
  WRITE_FLAG( false,                                      "sps_lmcs_enable_flag");
821
1.43k
  WRITE_FLAG( pcSPS->weightPred,                          "sps_weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
822
1.43k
  WRITE_FLAG( pcSPS->weightedBiPred,                      "sps_weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
823
1.43k
  WRITE_FLAG( pcSPS->longTermRefsPresent,                 "sps_long_term_ref_pics_flag" );
824
1.43k
  if( pcSPS->vpsId > 0 )
825
0
  {
826
0
    WRITE_FLAG( pcSPS->interLayerPresent,                 "sps_inter_layer_ref_pics_present_flag" );
827
0
  }
828
1.43k
  WRITE_FLAG( pcSPS->idrRefParamList,                     "sps_idr_rpl_present_flag" );
829
1.43k
  WRITE_FLAG( pcSPS->rpl1CopyFromRpl0,                    "sps_rpl1_copy_from_rpl0_flag");
830
831
  //Write candidate for List0
832
1.43k
  uint32_t numberOfRPL = (uint32_t)pcSPS->getNumRPL(0);
833
1.43k
  WRITE_UVLC(numberOfRPL,                                 "sps_num_ref_pic_lists_in_sps[0]");
834
35.8k
  for (int ii = 0; ii < numberOfRPL; ii++)
835
34.3k
  {
836
34.3k
    xCodeRefPicList( &pcSPS->rplList[0][ii], pcSPS->longTermRefsPresent, pcSPS->bitsForPOC, !pcSPS->weightPred && !pcSPS->weightedBiPred, ii );
837
34.3k
  }
838
839
  //Write candidate for List1
840
1.43k
  if (!pcSPS->rpl1CopyFromRpl0)
841
1.43k
  {
842
1.43k
    numberOfRPL = (uint32_t)pcSPS->getNumRPL(1);
843
1.43k
    WRITE_UVLC(numberOfRPL,                               "sps_num_ref_pic_lists_in_sps[1]");
844
35.8k
    for (int ii = 0; ii < numberOfRPL; ii++)
845
34.3k
    {
846
34.3k
      xCodeRefPicList( &pcSPS->rplList[1][ii], pcSPS->longTermRefsPresent, pcSPS->bitsForPOC, !pcSPS->weightPred && !pcSPS->weightedBiPred, ii );
847
34.3k
    }
848
1.43k
  }
849
850
1.43k
  WRITE_FLAG( pcSPS->wrapAroundEnabled,                   "sps_ref_wraparound_enabled_flag" );
851
852
1.43k
  WRITE_FLAG( pcSPS->temporalMVPEnabled,                  "sps_temporal_mvp_enabled_flag" );
853
854
1.43k
  if ( pcSPS->temporalMVPEnabled )
855
1.43k
  {
856
1.43k
    WRITE_FLAG( pcSPS->SbtMvp,                            "sps_sbtmvp_enabled_flag");
857
1.43k
  }
858
859
1.43k
  WRITE_FLAG( pcSPS->AMVR,                                "sps_amvr_enabled_flag" );
860
861
1.43k
  WRITE_FLAG( pcSPS->BDOF,                                "sps_bdof_enabled_flag" );
862
1.43k
  if (pcSPS->BDOF)
863
1.43k
  {
864
1.43k
    WRITE_FLAG(pcSPS->BdofPresent,                        "sps_bdof_pic_present_flag");
865
1.43k
  }
866
1.43k
  WRITE_FLAG( pcSPS->SMVD,                                "sps_smvd_enabled_flag" );
867
1.43k
  WRITE_FLAG( pcSPS->DMVR,                                "sps_dmvr_enabled_flag" );
868
1.43k
  if (pcSPS->DMVR)
869
1.43k
  {
870
1.43k
    WRITE_FLAG(pcSPS->DmvrPresent,                        "sps_dmvr_pic_present_flag");
871
1.43k
  }
872
1.43k
  WRITE_FLAG(pcSPS->MMVD,                                 "sps_mmvd_enabled_flag");
873
1.43k
  if ( pcSPS->MMVD )
874
1.43k
  {
875
1.43k
    WRITE_FLAG( pcSPS->fpelMmvd,                          "sps_fpel_mmvd_enabled_flag" );
876
1.43k
  }
877
1.43k
  WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSPS->maxNumMergeCand,  "sps_six_minus_max_num_merge_cand");
878
1.43k
  WRITE_FLAG( pcSPS->SBT,                                 "sps_sbt_enabled_flag" );
879
1.43k
  WRITE_FLAG( pcSPS->Affine,                              "sps_affine_enabled_flag" );
880
1.43k
  if ( pcSPS->Affine )
881
1.43k
  {
882
1.43k
    WRITE_UVLC(AFFINE_MRG_MAX_NUM_CANDS - pcSPS->maxNumAffineMergeCand, "five_minus_max_num_subblock_merge_cand");
883
1.43k
    WRITE_FLAG( pcSPS->AffineType,                        "sps_affine_type_flag" );
884
1.43k
    if (pcSPS->AMVR )
885
1.43k
    {
886
1.43k
      WRITE_FLAG( pcSPS->AffineAmvr,                      "sps_affine_amvr_enabled_flag" );
887
1.43k
    }
888
889
1.43k
    WRITE_FLAG( pcSPS->PROF,                              "sps_affine_prof_enabled_flag" );
890
1.43k
    if (pcSPS->PROF)
891
1.43k
    {
892
1.43k
      WRITE_FLAG(pcSPS->ProfPresent,                      "sps_prof_pic_present_flag" );
893
1.43k
    }
894
1.43k
  }
895
896
1.43k
  WRITE_FLAG(pcSPS->BCW,                                  "sps_bcw_enabled_flag");
897
898
1.43k
  WRITE_FLAG( pcSPS->CIIP,                                "sps_ciip_enabled_flag" );
899
900
1.43k
  if (pcSPS->maxNumMergeCand >= 2)
901
1.43k
  {
902
1.43k
    WRITE_FLAG(pcSPS->GEO,                                "sps_gpm_enabled_flag");
903
1.43k
    if (pcSPS->GEO && pcSPS->maxNumMergeCand >= 3)
904
1.43k
    {
905
1.43k
      WRITE_UVLC(pcSPS->maxNumMergeCand - pcSPS->maxNumGeoCand,   "sps_max_num_merge_cand_minus_max_num_gpm_cand");
906
1.43k
    }
907
1.43k
  }
908
909
1.43k
  WRITE_UVLC(pcSPS->log2ParallelMergeLevelMinus2,         "sps_log2_parallel_merge_level_minus2");
910
1.43k
  WRITE_FLAG( pcSPS->ISP,                                 "sps_isp_enabled_flag");
911
1.43k
  WRITE_FLAG( pcSPS->MRL,                                 "sps_mrl_enabled_flag");
912
1.43k
  WRITE_FLAG( pcSPS->MIP,                                 "sps_mip_enabled_flag");
913
1.43k
  if( pcSPS->chromaFormatIdc != CHROMA_400)
914
1.43k
  {
915
1.43k
    WRITE_FLAG( pcSPS->LMChroma,                          "sps_cclm_enabled_flag" );
916
1.43k
  }
917
1.43k
  if ( pcSPS->chromaFormatIdc == CHROMA_420 )
918
1.43k
  {
919
1.43k
    WRITE_FLAG( pcSPS->horCollocatedChroma,               "sps_chroma_horizontal_collocated_flag" );
920
1.43k
    WRITE_FLAG( pcSPS->verCollocatedChroma,               "sps_chroma_vertical_collocated_flag" );
921
1.43k
  }
922
923
1.43k
  WRITE_FLAG(pcSPS->PLT,                                  "sps_palette_enabled_flag" );
924
925
1.43k
  if (pcSPS->chromaFormatIdc == CHROMA_444 && pcSPS->log2MaxTbSize != 6)
926
0
  {
927
0
    WRITE_FLAG(pcSPS->useColorTrans,                      "sps_act_enabled_flag");
928
0
  }
929
1.43k
  if (pcSPS->transformSkip || pcSPS->PLT)
930
1.43k
  {
931
1.43k
    WRITE_UVLC(pcSPS->internalMinusInputBitDepth[CH_L],   "sps_internal_bit_depth_minus_input_bit_depth");
932
1.43k
  }
933
934
1.43k
  WRITE_FLAG(pcSPS->IBC,                                  "sps_ibc_enabled_flag");
935
1.43k
  if( pcSPS->IBC )
936
1.43k
  {
937
1.43k
    WRITE_UVLC(IBC_MRG_MAX_NUM_CANDS - pcSPS->maxNumIBCMergeCand, "six_minus_max_num_ibc_merge_cand");
938
1.43k
  }
939
940
1.43k
  WRITE_FLAG( pcSPS->LADF,                                "sps_ladf_enabled_flag" );
941
1.43k
  if ( pcSPS->LADF )
942
0
  {
943
0
    THROW("no support");
944
0
  }
945
946
1.43k
  WRITE_FLAG( pcSPS->scalingListEnabled,                  "sps_explicit_scaling_list_enabled_flag" );
947
1.43k
  if (pcSPS->LFNST && pcSPS->scalingListEnabled )
948
0
  {
949
0
    WRITE_FLAG(pcSPS->disableScalingMatrixForLfnstBlks,   "sps_scaling_matrix_for_lfnst_disabled_flag");
950
0
  }
951
1.43k
  if (pcSPS->useColorTrans && pcSPS->scalingListEnabled)
952
0
  {
953
0
    WRITE_FLAG(pcSPS->scalingMatrixAlternativeColourSpaceDisabled, "sps_scaling_matrix_for_alternative_colour_space_disabled_flag");
954
0
  }
955
1.43k
  if (pcSPS->scalingMatrixAlternativeColourSpaceDisabled)
956
0
  {
957
0
    WRITE_FLAG(pcSPS->scalingMatrixDesignatedColourSpace, "sps_scaling_matrix_designated_colour_space_flag");
958
0
  }
959
1.43k
  WRITE_FLAG(pcSPS->depQuantEnabled,                      "sps_dep_quant_enabled_flag");
960
1.43k
  WRITE_FLAG(pcSPS->signDataHidingEnabled,                "sps_sign_data_hiding_enabled_flag");
961
962
1.43k
  WRITE_FLAG( pcSPS->virtualBoundariesEnabled,            "sps_virtual_boundaries_enabled_flag" );
963
1.43k
  if( pcSPS->virtualBoundariesEnabled )
964
0
  {
965
0
    WRITE_CODE( pcSPS->numVerVirtualBoundaries, 2,        "sps_num_ver_virtual_boundaries");
966
0
    for( unsigned i = 0; i < pcSPS->numVerVirtualBoundaries; i++ )
967
0
    {
968
0
      WRITE_UVLC((pcSPS->virtualBoundariesPosX[i]>>3),    "sps_virtual_boundaries_pos_x");
969
0
    }
970
0
    WRITE_CODE(pcSPS->numHorVirtualBoundaries, 2,         "sps_num_hor_virtual_boundaries");
971
0
    for( unsigned i = 0; i < pcSPS->numHorVirtualBoundaries; i++ )
972
0
    {
973
0
      WRITE_UVLC((pcSPS->virtualBoundariesPosY[i]>>3),    "sps_virtual_boundaries_pos_y");
974
0
    }
975
0
  }
976
977
1.43k
  if (pcSPS->ptlDpbHrdParamsPresent)
978
1.43k
  {
979
1.43k
    WRITE_FLAG(pcSPS->hrdParametersPresent,               "sps_timing_hrd_params_present_flag");
980
981
1.43k
    if( pcSPS->hrdParametersPresent )
982
1.43k
    {
983
1.43k
      codeGeneralHrdparameters(&pcSPS->generalHrdParams);
984
1.43k
      if ((pcSPS->maxTLayers - 1) > 0)
985
1.43k
      {
986
1.43k
        WRITE_FLAG(pcSPS->subLayerParametersPresent,      "sps_sublayer_cpb_params_present_flag");
987
1.43k
      }
988
1.43k
      uint32_t firstSubLayer = pcSPS->subLayerParametersPresent ? 0 : (pcSPS->maxTLayers - 1);
989
1.43k
      codeOlsHrdParameters(&pcSPS->generalHrdParams, pcSPS->olsHrdParams, firstSubLayer, pcSPS->maxTLayers - 1);
990
1.43k
    }
991
1.43k
  }
992
993
1.43k
  WRITE_FLAG(pcSPS->fieldSeqFlag,                         "sps_field_seq_flag");
994
995
1.43k
  WRITE_FLAG( pcSPS->vuiParametersPresent,                "sps_vui_parameters_present_flag" );
996
1.43k
  if (pcSPS->vuiParametersPresent)
997
0
  {
998
0
    OutputBitstream *bs = m_pcBitIf; // save the original ono
999
0
    OutputBitstream bs_count;
1000
0
    setBitstream(&bs_count);
1001
#if ENABLE_TRACING
1002
    bool traceEnable = g_HLSTraceEnable;
1003
    g_HLSTraceEnable = false;
1004
#endif
1005
0
    codeVUI(&pcSPS->vuiParameters, pcSPS);
1006
#if ENABLE_TRACING
1007
    g_HLSTraceEnable = traceEnable;
1008
#endif
1009
0
    unsigned vui_payload_data_num_bits = bs_count.getNumberOfWrittenBits();
1010
0
    CHECK( vui_payload_data_num_bits % 8 != 0, "Invalid number of VUI payload data bits" );
1011
0
    setBitstream(bs);
1012
0
    WRITE_UVLC((vui_payload_data_num_bits >> 3) - 1,        "sps_vui_payload_size_minus1");
1013
0
    while (!isByteAligned())
1014
0
    {
1015
0
      WRITE_FLAG(0,                                         "sps_vui_alignment_zero_bit");
1016
0
    }
1017
0
    codeVUI(&pcSPS->vuiParameters, pcSPS);
1018
0
  }
1019
1020
1.43k
  bool sps_extension_present_flag=false;
1021
1.43k
  bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS]={false};
1022
1023
12.8k
  for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
1024
11.4k
  {
1025
11.4k
    sps_extension_present_flag|=sps_extension_flags[i];
1026
11.4k
  }
1027
1028
1.43k
  WRITE_FLAG( sps_extension_present_flag,                   "sps_extension_present_flag" );
1029
1030
1.43k
  if (sps_extension_present_flag)
1031
0
  {
1032
#if ENABLE_TRACING
1033
    static const char *syntaxStrings[]={ "sps_range_extension_flag",
1034
      "sps_multilayer_extension_flag",
1035
      "sps_extension_6bits[0]",
1036
      "sps_extension_6bits[1]",
1037
      "sps_extension_6bits[2]",
1038
      "sps_extension_6bits[3]",
1039
      "sps_extension_6bits[4]",
1040
      "sps_extension_6bits[5]" };
1041
#endif
1042
1043
0
    for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
1044
0
    {
1045
0
      WRITE_FLAG( sps_extension_flags[i], syntaxStrings[i] );
1046
0
    }
1047
1048
0
    for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
1049
0
    {
1050
0
      if (sps_extension_flags[i])
1051
0
      {
1052
#if 0 // TODO: enable when applicable
1053
        switch (SPSExtensionFlagIndex(i))
1054
        {
1055
        default:
1056
          CHECK(sps_extension_flags[i]!=false, "Unknown PPS extension signalled"); // Should never get here with an active SPS extension flag.
1057
          break;
1058
        }
1059
#endif
1060
0
      }
1061
0
    }
1062
0
  }
1063
1.43k
  xWriteRbspTrailingBits();
1064
1.43k
}
1065
1066
void HLSWriter::codeDCI( const DCI* dci )
1067
0
{
1068
0
  DTRACE( g_trace_ctx, D_HEADER, "=========== Decoding Parameter Set     ===========\n" );
1069
1070
0
  WRITE_CODE( 0,                                    4,        "dci_reserved_zero_5bits" );
1071
0
  uint32_t numPTLs = (uint32_t) dci->profileTierLevel.size();
1072
0
  CHECK (numPTLs<1, "At least one PTL must be available in DPS");
1073
1074
0
  WRITE_CODE( numPTLs - 1,                          4,        "dci_num_ptls_minus1" );
1075
1076
0
  for (int i=0; i< numPTLs; i++)
1077
0
  {
1078
0
    codeProfileTierLevel( &dci->profileTierLevel[i], true, 0 );
1079
0
  }
1080
0
  WRITE_FLAG( 0,                                              "dci_extension_flag" );
1081
0
  xWriteRbspTrailingBits();
1082
0
}
1083
1084
void HLSWriter::codeVPS(const VPS* pcVPS)
1085
0
{
1086
0
  DTRACE( g_trace_ctx, D_HEADER, "=========== Video Parameter Set     ===========\n" );
1087
1088
0
  WRITE_CODE(pcVPS->vpsId,              4,              "vps_video_parameter_set_id");
1089
0
  WRITE_CODE(pcVPS->maxLayers - 1,      6,              "vps_max_layers_minus1");
1090
0
  WRITE_CODE(pcVPS->maxSubLayers - 1,   3,              "vps_max_sublayers_minus1");
1091
0
  if (pcVPS->maxLayers > 1 && pcVPS->maxSubLayers > 1)
1092
0
  {
1093
0
    WRITE_FLAG(pcVPS->defaultPtlDpbHrdMaxTidFlag,        "vps_default_ptl_dpb_hrd_max_tid_flag");
1094
0
  }
1095
0
  if (pcVPS->maxLayers > 1)
1096
0
  {
1097
0
    WRITE_FLAG(pcVPS->allIndependentLayers,             "vps_all_independent_layers_flag");
1098
0
  }
1099
0
  for (uint32_t i = 0; i < pcVPS->maxLayers; i++)
1100
0
  {
1101
0
    WRITE_CODE(pcVPS->layerId[i], 6,                    "vps_layer_id");
1102
0
    if (i > 0 && !pcVPS->allIndependentLayers)
1103
0
    {
1104
0
      WRITE_FLAG(pcVPS->independentLayer[i],            "vps_independent_layer_flag");
1105
0
      if (!pcVPS->independentLayer[i])
1106
0
      {
1107
0
        bool presentFlag = false;
1108
0
        for (int j = 0; j < i; j++)
1109
0
        {
1110
0
          presentFlag |= ((pcVPS->maxTidIlRefPicsPlus1[i][j] != VVENC_MAX_TLAYER) && pcVPS->directRefLayer[i][j]);
1111
0
        }
1112
0
        WRITE_FLAG(presentFlag, "max_tid_ref_present_flag[ i ]");
1113
0
        for (int j = 0; j < i; j++)
1114
0
        {
1115
0
          WRITE_FLAG(pcVPS->directRefLayer[i][j], "vps_direct_ref_layer_flag");
1116
0
          if (presentFlag && pcVPS->directRefLayer[i][j])
1117
0
          {
1118
0
            WRITE_CODE(pcVPS->maxTidIlRefPicsPlus1[i][j], 3, "max_tid_il_ref_pics_plus1[ i ][ j ]");
1119
0
          }
1120
0
        }
1121
0
      }
1122
0
    }
1123
0
  }
1124
0
  if( pcVPS->maxLayers > 1 )
1125
0
  {
1126
0
    if (pcVPS->allIndependentLayers)
1127
0
    {
1128
0
      WRITE_FLAG(pcVPS->eachLayerIsAnOls,               "vps_each_layer_is_an_ols_flag");
1129
0
    }
1130
0
    if (!pcVPS->eachLayerIsAnOls)
1131
0
    {
1132
0
      if (!pcVPS->allIndependentLayers)
1133
0
      {
1134
0
        WRITE_CODE(pcVPS->olsModeIdc, 2,                "vps_ols_mode_idc");
1135
0
      }
1136
0
      if (pcVPS->olsModeIdc == 2)
1137
0
      {
1138
0
        WRITE_CODE(pcVPS->numOutputLayerSets - 2, 8,    "vps_num_output_layer_sets_minus2");
1139
0
        for (uint32_t i = 1; i < pcVPS->numOutputLayerSets; i++)
1140
0
        {
1141
0
          for (uint32_t j = 0; j < pcVPS->maxLayers; j++)
1142
0
          {
1143
0
            WRITE_FLAG(pcVPS->olsOutputLayer[i][j],     "vps_ols_output_layer_flag");
1144
0
          }
1145
0
        }
1146
0
      }
1147
0
    }
1148
0
    CHECK(pcVPS->numPtls - 1 >= pcVPS->totalNumOLSs, "vps_num_ptls_minus1 shall be less than TotalNumOlss");
1149
0
    WRITE_CODE(pcVPS->numPtls - 1, 8,                   "vps_num_ptls_minus1");
1150
0
  }
1151
1152
0
  int totalNumOlss = pcVPS->totalNumOLSs;
1153
0
  for (int i = 0; i < pcVPS->numPtls; i++)
1154
0
  {
1155
0
    if(i > 0)
1156
0
    {
1157
0
      WRITE_FLAG(pcVPS->ptPresent[i],                   "vps_ptl_present_flag");
1158
0
    }
1159
0
    if(!pcVPS->allLayersSameNumSubLayers)
1160
0
    {
1161
0
      WRITE_CODE(pcVPS->ptlMaxTemporalId[i] ,3,         "vps_ptl_max_temporal_id");
1162
0
    }
1163
0
  }
1164
0
  int cnt = 0;
1165
0
  while (m_pcBitIf->getNumBitsUntilByteAligned())
1166
0
  {
1167
0
    WRITE_FLAG( 0,                                      "vps_ptl_reserved_zero_bit");
1168
0
    cnt++;
1169
0
  }
1170
0
  CHECK(cnt>=8, "More than '8' alignment bytes written");
1171
0
  for (int i = 0; i < pcVPS->numPtls; i++)
1172
0
  {
1173
0
    codeProfileTierLevel(&pcVPS->profileTierLevel[i], pcVPS->ptPresent[i], pcVPS->ptlMaxTemporalId[i] - 1);
1174
0
  }
1175
0
  for (int i = 0; i < totalNumOlss; i++)
1176
0
  {
1177
0
    if(pcVPS->numPtls > 1 && pcVPS->numPtls != pcVPS->totalNumOLSs)
1178
0
      WRITE_CODE(pcVPS->olsPtlIdx[i], 8,                "vps_ols_ptl_idx");
1179
0
  }
1180
0
  if( !pcVPS->allIndependentLayers )
1181
0
  {
1182
0
    WRITE_UVLC( pcVPS->numDpbParams,                    "vps_num_dpb_params" );
1183
0
  }
1184
1185
0
  if( pcVPS->numDpbParams > 0 && pcVPS->maxSubLayers > 1 )
1186
0
  {
1187
0
    WRITE_FLAG( pcVPS->sublayerDpbParamsPresent,        "vps_sublayer_dpb_params_present_flag" );
1188
0
  }
1189
1190
0
  for( int i = 0; i < pcVPS->numDpbParams; i++ )
1191
0
  {
1192
0
    if( !pcVPS->allLayersSameNumSubLayers )
1193
0
    {
1194
0
      WRITE_CODE( pcVPS->dpbMaxTemporalId[i], 3,      "vps_dpb_max_temporal_id[i]" );
1195
0
    }
1196
0
    if( pcVPS->maxSubLayers == 1 )
1197
0
    {
1198
0
      CHECK( pcVPS->dpbMaxTemporalId[i] != 0, "When vps_max_sublayers_minus1 is equal to 0, the value of dpb_max_temporal_id[ i ] is inferred to be equal to 0" );
1199
0
    }
1200
0
    else
1201
0
    {
1202
0
      if( pcVPS->defaultPtlDpbHrdMaxTidFlag )
1203
0
      {
1204
0
        CHECK( pcVPS->dpbMaxTemporalId[i] != pcVPS->maxSubLayers - 1, "When vps_max_sublayers_minus1 is greater than 0 and vps_all_layers_same_num_sublayers_flag is equal to 1, the value of dpb_max_temporal_id[ i ] is inferred to be equal to vps_max_sublayers_minus1" );
1205
0
      }
1206
0
      else
1207
0
      {
1208
0
        WRITE_CODE( pcVPS->dpbMaxTemporalId[i], 3,      "vps_dpb_max_temporal_id[i]" );
1209
0
      }
1210
0
    }
1211
1212
0
    for( int j = ( pcVPS->sublayerDpbParamsPresent ? 0 : pcVPS->dpbMaxTemporalId[i] ); j <= pcVPS->dpbMaxTemporalId[i]; j++ )
1213
0
    {
1214
0
      WRITE_UVLC( pcVPS->dpbParameters[i].maxDecPicBuffering[j],      "max_dec_pic_buffering_minus1[i]" );
1215
0
      WRITE_UVLC( pcVPS->dpbParameters[i].numReorderPics[j],          "max_num_reorder_pics[i]" );
1216
0
      WRITE_UVLC( pcVPS->dpbParameters[i].maxLatencyIncreasePlus1[j], "max_latency_increase_plus1[i]" );
1217
0
    }
1218
0
  }
1219
1220
0
  for( int i = 0; i < pcVPS->totalNumOLSs; i++ )
1221
0
  {
1222
0
    if( pcVPS->numLayersInOls[i] > 1 )
1223
0
    {
1224
0
      WRITE_UVLC( pcVPS->olsDpbPicSize[i].width,          "vps_ols_dpb_pic_width[i]" );
1225
0
      WRITE_UVLC( pcVPS->olsDpbPicSize[i].height,         "vps_ols_dpb_pic_height[i]" );
1226
0
      WRITE_CODE( pcVPS->olsDpbChromaFormatIdc[i], 2,     "vps_ols_dpb_chroma_format[i]");
1227
0
      WRITE_UVLC( pcVPS->olsDpbBitDepthMinus8[i],         "vps_ols_dpb_bitdepth_minus8[i]");
1228
0
      if( pcVPS->numDpbParams > 1 && (pcVPS->numDpbParams != pcVPS->numMultiLayeredOlss) )
1229
0
      {
1230
0
        WRITE_UVLC( pcVPS->olsDpbParamsIdx[i],            "vps_ols_dpb_params_idx[i]" );
1231
0
      }
1232
0
    }
1233
0
  }
1234
1235
1236
0
  if (!pcVPS->eachLayerIsAnOls)
1237
0
  {
1238
0
    WRITE_FLAG(pcVPS->generalHrdParamsPresent, "vps_general_hrd_params_present_flag");
1239
0
  }
1240
0
  if (pcVPS->generalHrdParamsPresent)
1241
0
  {
1242
0
    codeGeneralHrdparameters(&pcVPS->generalHrdParams);
1243
0
    if ((pcVPS->maxSubLayers-1) > 0)
1244
0
    {
1245
0
      WRITE_FLAG(pcVPS->sublayerCpbParamsPresent, "vps_sublayer_cpb_params_present_flag");
1246
0
    }
1247
0
    WRITE_UVLC(pcVPS->numOlsHrdParamsMinus1, "vps_num_ols_hrd_params_minus1");
1248
0
    for (int i = 0; i <= pcVPS->numOlsHrdParamsMinus1; i++)
1249
0
    {
1250
0
      if (!pcVPS->defaultPtlDpbHrdMaxTidFlag)
1251
0
      {
1252
0
        WRITE_CODE(pcVPS->hrdMaxTid[i], 3, "vps_hrd_vps_max_tid[i]");
1253
0
      }
1254
0
      uint32_t firstSublayer = pcVPS->sublayerCpbParamsPresent ? 0 : pcVPS->hrdMaxTid[i];
1255
0
      codeOlsHrdParameters(&pcVPS->generalHrdParams, &pcVPS->olsHrdParams[i], firstSublayer, pcVPS->hrdMaxTid[i]);
1256
0
    }
1257
0
    if ((pcVPS->numOlsHrdParamsMinus1 > 0) && ((pcVPS->numOlsHrdParamsMinus1 + 1) != pcVPS->numMultiLayeredOlss))
1258
0
    {
1259
0
      for (int i = 0; i < pcVPS->numMultiLayeredOlss; i++)
1260
0
      {
1261
0
        WRITE_UVLC(pcVPS->olsHrdIdx[i], "vps_ols_hrd_idx[i]");
1262
0
      }
1263
0
    }
1264
0
  }
1265
0
  WRITE_FLAG(0,                                           "vps_extension_flag");
1266
1267
  //future extensions here..
1268
0
  xWriteRbspTrailingBits();
1269
0
}
1270
1271
void HLSWriter::codePictureHeader( const PicHeader* picHeader, bool writeRbspTrailingBits )
1272
1.43k
{
1273
1.43k
  const PPS*  pps = NULL;
1274
1.43k
  const SPS*  sps = NULL;
1275
1276
1.43k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Picture Header ===========\n" );
1277
1278
1.43k
  CodingStructure& cs = *picHeader->pic->cs;
1279
1.43k
  WRITE_FLAG(picHeader->gdrOrIrapPic, "ph_gdr_or_irap_pic_flag");
1280
1.43k
  WRITE_FLAG(picHeader->nonRefPic,    "ph_non_ref_pic_flag");
1281
1.43k
  if (picHeader->gdrOrIrapPic)
1282
1.43k
  {
1283
1.43k
    WRITE_FLAG(picHeader->gdrPic,     "ph_gdr_pic_flag");
1284
1.43k
  }
1285
  // Q0781, two-flags
1286
1.43k
  WRITE_FLAG(picHeader->picInterSliceAllowed,   "ph_inter_slice_allowed_flag");
1287
1.43k
  if (picHeader->picInterSliceAllowed)
1288
0
  {
1289
0
    WRITE_FLAG(picHeader->picIntraSliceAllowed, "ph_intra_slice_allowed_flag");
1290
0
  }
1291
  // parameter sets
1292
1.43k
  WRITE_UVLC(picHeader->ppsId,                  "ph_pic_parameter_set_id");
1293
1.43k
  pps = cs.slice->pps;
1294
1.43k
  CHECK(pps == 0, "Invalid PPS");
1295
1.43k
  sps = cs.slice->sps;
1296
1.43k
  CHECK(sps == 0, "Invalid SPS");
1297
1.43k
  int pocBits = cs.slice->sps->bitsForPOC;
1298
1.43k
  int pocMask = (1 << pocBits) - 1;
1299
1.43k
  WRITE_CODE(cs.slice->poc & pocMask, pocBits,  "ph_pic_order_cnt_lsb");
1300
1.43k
  if( picHeader->gdrPic )
1301
0
  {
1302
0
    WRITE_UVLC(picHeader->recoveryPocCnt,       "ph_recovery_poc_cnt");
1303
0
  }
1304
1305
  // PH extra bits are not written in the reference encoder
1306
  // as these bits are reserved for future extensions
1307
  // for( i = 0; i < NumExtraPhBits; i++ )
1308
  //    ph_extra_bit[ i ]
1309
1310
1.43k
  if (sps->pocMsbFlag)
1311
0
  {
1312
0
    WRITE_FLAG(picHeader->pocMsbPresent,        "ph_poc_msb_present_flag");
1313
0
    if (picHeader->pocMsbPresent)
1314
0
    {
1315
0
      WRITE_CODE(picHeader->pocMsbVal, sps->pocMsbLen, "ph_poc_msb_val");
1316
0
    }
1317
0
  }
1318
1319
   // alf enable flags and aps IDs
1320
1.43k
  if( sps->alfEnabled)
1321
1.43k
  {
1322
1.43k
    if (pps->alfInfoInPh)
1323
0
    {
1324
0
      WRITE_FLAG(picHeader->alfEnabled[COMP_Y],         "ph_alf_enabled_flag");
1325
0
      if (picHeader->alfEnabled[COMP_Y])
1326
0
      {
1327
0
        WRITE_CODE(picHeader->numAlfAps, 3,             "ph_num_alf_aps_ids_luma");
1328
0
        for (int i = 0; i < picHeader->numAlfAps; i++)
1329
0
        {
1330
0
          WRITE_CODE(picHeader->alfApsId[i], 3,         "ph_alf_aps_id_luma");
1331
0
        }
1332
1333
0
        const int alfChromaIdc = picHeader->alfEnabled[COMP_Cb] + picHeader->alfEnabled[COMP_Cr] * 2 ;
1334
0
        if (sps->chromaFormatIdc != CHROMA_400)
1335
0
        {
1336
0
          WRITE_CODE(picHeader->alfEnabled[COMP_Cb], 1, "ph_alf_cb_enabled_flag");
1337
0
          WRITE_CODE(picHeader->alfEnabled[COMP_Cr], 1, "ph_alf_cr_enabled_flag");
1338
0
        }
1339
0
        if (alfChromaIdc)
1340
0
        {
1341
0
          WRITE_CODE(picHeader->alfChromaApsId, 3,      "ph_alf_aps_id_chroma");
1342
0
        }
1343
0
        if (sps->ccalfEnabled)
1344
0
        {
1345
0
          WRITE_FLAG(picHeader->ccalfEnabled[COMP_Cb],  "ph_cc_alf_cb_enabled_flag");
1346
0
          if (picHeader->ccalfEnabled[COMP_Cb])
1347
0
          {
1348
0
            WRITE_CODE(picHeader->ccalfCbApsId, 3,      "ph_cc_alf_cb_aps_id");
1349
0
          }
1350
0
          WRITE_FLAG(picHeader->ccalfEnabled[COMP_Cr],  "ph_cc_alf_cr_enabled_flag");
1351
0
          if (picHeader->ccalfEnabled[COMP_Cr])
1352
0
          {
1353
0
            WRITE_CODE(picHeader->ccalfCrApsId, 3,      "ph_cc_alf_cr_aps_id");
1354
0
          }
1355
0
        }
1356
0
      }
1357
0
    }
1358
1.43k
  }
1359
1360
  // quantization scaling lists
1361
1.43k
  if( sps->scalingListEnabled )
1362
0
  {
1363
0
    WRITE_FLAG( picHeader->explicitScalingListEnabled,  "ph_scaling_list_present_flag" );
1364
0
    if( picHeader->explicitScalingListEnabled )
1365
0
    {
1366
0
      WRITE_CODE( picHeader->scalingListApsId, 3,       "ph_scaling_list_aps_id" );
1367
0
    }
1368
0
  }
1369
1370
  // virtual boundaries
1371
1.43k
  if( sps->virtualBoundariesEnabled && !sps->virtualBoundariesPresent )
1372
0
  {
1373
0
    WRITE_FLAG( picHeader->virtualBoundariesEnabled,    "ph_loop_filter_across_virtual_boundaries_disabled_present_flag" );
1374
0
    if( picHeader->virtualBoundariesEnabled )
1375
0
    {
1376
0
      WRITE_CODE(picHeader->numVerVirtualBoundaries, 2, "ph_num_ver_virtual_boundaries");
1377
0
      for( unsigned i = 0; i < picHeader->numVerVirtualBoundaries; i++ )
1378
0
      {
1379
0
        WRITE_UVLC(picHeader->virtualBoundariesPosX[i] >> 3, "ph_virtual_boundaries_pos_x");
1380
0
      }
1381
0
      WRITE_CODE(picHeader->numHorVirtualBoundaries, 2, "ph_num_hor_virtual_boundaries");
1382
0
      for( unsigned i = 0; i < picHeader->numHorVirtualBoundaries; i++ )
1383
0
      {
1384
0
        WRITE_UVLC(picHeader->virtualBoundariesPosY[i]>>3, "ph_virtual_boundaries_pos_y");
1385
0
      }
1386
0
    }
1387
0
  }
1388
1389
  // picture output flag
1390
1.43k
  if( pps->outputFlagPresent && !picHeader->nonRefPic)
1391
0
  {
1392
0
    WRITE_FLAG( picHeader->picOutputFlag, "ph_pic_output_flag" );
1393
0
  }
1394
1395
  // reference picture lists
1396
1.43k
  if (pps->rplInfoInPh)
1397
0
  {
1398
    // List0 and List1
1399
0
    for(int listIdx = 0; listIdx < 2; listIdx++)
1400
0
    {
1401
0
      if(sps->getNumRPL(listIdx) > 0 &&
1402
0
          (listIdx == 0 || (listIdx == 1 && pps->rpl1IdxPresent)))
1403
0
      {
1404
0
        WRITE_FLAG(picHeader->rplIdx[listIdx] != -1 ? 1 : 0, "pic_rpl_sps_flag[i]");
1405
0
      }
1406
0
      else if(sps->getNumRPL(listIdx) == 0)
1407
0
      {
1408
0
        CHECK(picHeader->rplIdx[listIdx] != -1, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1409
0
      }
1410
0
      else if(listIdx == 1)
1411
0
      {
1412
0
        auto rplsSpsFlag0 = picHeader->rplIdx[0] != -1 ? 1 : 0;
1413
0
        auto rplsSpsFlag1 = picHeader->rplIdx[1] != -1 ? 1 : 0;
1414
0
        CHECK(rplsSpsFlag1 != rplsSpsFlag0, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1415
0
      }
1416
1417
0
      if(picHeader->rplIdx[listIdx] != -1)
1418
0
      {
1419
0
        if(sps->getNumRPL(listIdx) > 1 &&
1420
0
            (listIdx == 0 || (listIdx == 1 && pps->rpl1IdxPresent)))
1421
0
        {
1422
0
          int numBits = ceilLog2(sps->getNumRPL( listIdx ));
1423
0
          WRITE_CODE(picHeader->rplIdx[listIdx], numBits, "pic_rpl_idx[i]");
1424
0
        }
1425
0
        else if(sps->getNumRPL(listIdx) == 1)
1426
0
        {
1427
0
          CHECK(picHeader->rplIdx[listIdx] != 0, "RPL1Idx is not signalled but it is not equal to 0");
1428
0
        }
1429
0
        else
1430
0
        {
1431
0
          CHECK(picHeader->rplIdx[1] != picHeader->rplIdx[0], "RPL1Idx is not signalled but it is not the same as RPL0Idx");
1432
0
        }
1433
0
      }
1434
      // explicit RPL in picture header
1435
0
      else
1436
0
      {
1437
0
        xCodeRefPicList( picHeader->pRPL[listIdx], sps->longTermRefsPresent, sps->bitsForPOC, !(sps->weightPred||sps->weightedBiPred), -1 );
1438
0
      }
1439
1440
      // POC MSB cycle signalling for LTRP
1441
0
      if (picHeader->pRPL[listIdx]->numberOfLongtermPictures)
1442
0
      {
1443
0
        for (int i = 0; i < picHeader->pRPL[listIdx]->numberOfLongtermPictures + picHeader->pRPL[listIdx]->numberOfShorttermPictures; i++)
1444
0
        {
1445
0
          if (picHeader->pRPL[listIdx]->isLongtermRefPic[i])
1446
0
          {
1447
0
            if (picHeader->pRPL[listIdx]->ltrpInSliceHeader)
1448
0
            {
1449
0
              WRITE_CODE(picHeader->pRPL[listIdx]->refPicIdentifier[i], sps->bitsForPOC,
1450
0
                         "pic_poc_lsb_lt[listIdx][rplsIdx][j]");
1451
0
            }
1452
0
            WRITE_FLAG(picHeader->pRPL[listIdx]->deltaPocMSBPresent[i], "pic_delta_poc_msb_present_flag[i][j]");
1453
0
            if (picHeader->pRPL[listIdx]->deltaPocMSBPresent[i])
1454
0
            {
1455
0
              WRITE_UVLC(picHeader->pRPL[listIdx]->deltaPocMSBCycleLT[i], "pic_delta_poc_msb_cycle_lt[i][j]");
1456
0
            }
1457
0
          }
1458
0
        }
1459
0
      }
1460
0
    }
1461
0
  }
1462
1463
  // partitioning constraint overrides
1464
1.43k
  if (sps->partitionOverrideEnabled )
1465
1.43k
  {
1466
1.43k
    WRITE_FLAG(picHeader->splitConsOverride, "partition_constraints_override_flag");
1467
1.43k
  }
1468
1469
  // Q0781, two-flags
1470
1.43k
  if (picHeader->picIntraSliceAllowed)
1471
1.43k
  {
1472
1.43k
    if (picHeader->splitConsOverride)
1473
0
    {
1474
0
      WRITE_UVLC(floorLog2(picHeader->minQTSize[0]) - sps->log2MinCodingBlockSize, "pic_log2_diff_min_qt_min_cb_intra_slice_luma");
1475
0
      WRITE_UVLC(picHeader->maxMTTDepth[0], "ph_max_mtt_hierarchy_depth_intra_slice_luma");
1476
0
      if (picHeader->maxMTTDepth[0] != 0)
1477
0
      {
1478
0
        WRITE_UVLC(floorLog2(picHeader->maxBTSize[0]) - floorLog2(picHeader->minQTSize[0]), "ph_log2_diff_max_bt_min_qt_intra_slice_luma");
1479
0
        WRITE_UVLC(floorLog2(picHeader->maxTTSize[0]) - floorLog2(picHeader->minQTSize[0]), "ph_log2_diff_max_tt_min_qt_intra_slice_luma");
1480
0
      }
1481
1482
0
      if (sps->dualITree)
1483
0
      {
1484
0
        WRITE_UVLC(floorLog2(picHeader->minQTSize[2]) - sps->log2MinCodingBlockSize, "ph_log2_diff_min_qt_min_cb_intra_slice_chroma");
1485
0
        WRITE_UVLC(picHeader->maxMTTDepth[2], "ph_max_mtt_hierarchy_depth_intra_slice_chroma");
1486
0
        if (picHeader->maxMTTDepth[2] != 0)
1487
0
        {
1488
0
          WRITE_UVLC(floorLog2(picHeader->maxBTSize[2]) - floorLog2(picHeader->minQTSize[2]), "ph_log2_diff_max_bt_min_qt_intra_slice_chroma");
1489
0
          WRITE_UVLC(floorLog2(picHeader->maxTTSize[2]) - floorLog2(picHeader->minQTSize[2]), "ph_log2_diff_max_tt_min_qt_intra_slice_chroma");
1490
0
        }
1491
0
      }
1492
0
    }
1493
1.43k
  }
1494
1.43k
  if (picHeader->picIntraSliceAllowed )
1495
1.43k
  {
1496
  // delta quantization and chrom and chroma offset
1497
1.43k
    if (pps->useDQP)
1498
1.43k
    {
1499
1.43k
      WRITE_UVLC( picHeader->cuQpDeltaSubdivIntra, "ph_cu_qp_delta_subdiv_intra_slice" );
1500
1.43k
    }
1501
1.43k
    if (pps->chromaQpOffsetListLen )
1502
0
    {
1503
0
      WRITE_UVLC( picHeader->cuChromaQpOffsetSubdivIntra, "ph_cu_chroma_qp_offset_subdiv_intra_slice" );
1504
0
    }
1505
1.43k
  }
1506
1507
1.43k
  if (picHeader->picInterSliceAllowed )
1508
0
  {
1509
0
    if (picHeader->splitConsOverride )
1510
0
    {
1511
0
      WRITE_UVLC(floorLog2(picHeader->minQTSize[1]) - sps->log2MinCodingBlockSize, "ph_log2_diff_min_qt_min_cb_inter_slice");
1512
0
      WRITE_UVLC(picHeader->maxMTTDepth[1], "ph_max_mtt_hierarchy_depth_inter_slice");
1513
0
      if (picHeader->maxMTTDepth[1] != 0)
1514
0
      {
1515
0
        WRITE_UVLC(floorLog2(picHeader->maxBTSize[1]) - floorLog2(picHeader->minQTSize[1]), "ph_log2_diff_max_bt_min_qt_inter_slice");
1516
0
        WRITE_UVLC(floorLog2(picHeader->maxTTSize[1]) - floorLog2(picHeader->minQTSize[1]), "ph_log2_diff_max_tt_min_qt_inter_slice");
1517
0
      }
1518
0
    }
1519
1520
    // delta quantization and chrom and chroma offset
1521
0
    if (pps->useDQP)
1522
0
    {
1523
0
      WRITE_UVLC(picHeader->cuQpDeltaSubdivInter, "ph_cu_qp_delta_subdiv_inter_slice");
1524
0
    }
1525
1526
0
    if (pps->chromaQpOffsetListLen )
1527
0
    {
1528
0
      WRITE_UVLC(picHeader->cuChromaQpOffsetSubdivInter, "ph_cu_chroma_qp_offset_subdiv_inter_slice");
1529
0
    }
1530
1531
    // temporal motion vector prediction
1532
0
    if (sps->temporalMVPEnabled)
1533
0
    {
1534
0
      WRITE_FLAG( picHeader->enableTMVP, "ph_temporal_mvp_enabled_flag" );
1535
0
      if (picHeader->enableTMVP && pps->rplInfoInPh)
1536
0
      {
1537
0
        if (picHeader->pRPL[1]->getNumRefEntries() > 0)
1538
0
        {
1539
0
          WRITE_CODE(picHeader->picColFromL0, 1, "ph_collocated_from_l0_flag");
1540
0
        }
1541
0
        if ((picHeader->picColFromL0 && picHeader->pRPL[0]->getNumRefEntries() > 1) ||
1542
0
          (!picHeader->picColFromL0 && picHeader->pRPL[1]->getNumRefEntries() > 1))
1543
0
        {
1544
0
          WRITE_UVLC(picHeader->colRefIdx, "ph_collocated_ref_idx");
1545
0
        }
1546
0
      }
1547
0
    }
1548
1549
  // full-pel MMVD flag
1550
0
    if (sps->fpelMmvd )
1551
0
    {
1552
0
      WRITE_FLAG( picHeader->disFracMMVD, "ph_fpel_mmvd_enabled_flag" );
1553
0
    }
1554
  // mvd L1 zero flag
1555
0
    if (!pps->rplInfoInPh || picHeader->pRPL[1]->getNumRefEntries() > 0)
1556
0
    {
1557
0
      WRITE_FLAG(picHeader->mvdL1Zero, "ph_mvd_l1_zero_flag");
1558
0
    }
1559
1560
  // picture level BDOF disable flags
1561
0
    if (sps->BdofPresent && (!pps->rplInfoInPh || picHeader->pRPL[1]->getNumRefEntries() > 0))
1562
0
    {
1563
0
      WRITE_FLAG(picHeader->disBdofFlag, "ph_disable_bdof_flag");
1564
0
    }
1565
1566
  // picture level DMVR disable flags
1567
0
    if (sps->DmvrPresent && (!pps->rplInfoInPh || picHeader->pRPL[1]->getNumRefEntries() > 0))
1568
0
    {
1569
0
      WRITE_FLAG(picHeader->disDmvrFlag, "ph_disable_dmvr_flag");
1570
0
    }
1571
1572
  // picture level PROF disable flags
1573
0
    if (sps->ProfPresent)
1574
0
    {
1575
0
      WRITE_FLAG(picHeader->disProfFlag, "ph_disable_prof_flag");
1576
0
    }
1577
1578
0
    if ((pps->weightPred || pps->weightedBiPred) && pps->wpInfoInPh )
1579
0
    {
1580
0
      xCodePredWeightTable(picHeader, pps, sps);
1581
0
    }
1582
0
   }
1583
1584
1.43k
  if (pps->qpDeltaInfoInPh)
1585
0
  {
1586
0
    WRITE_SVLC(picHeader->qpDelta, "ph_qp_delta");
1587
0
  }
1588
1589
  // joint Cb/Cr sign flag
1590
1.43k
  if (sps->jointCbCr )
1591
1.43k
  {
1592
1.43k
    WRITE_FLAG( picHeader->jointCbCrSign, "ph_joint_cbcr_sign_flag" );
1593
1.43k
  }
1594
1595
  // sao enable flags
1596
1.43k
  if(sps->saoEnabled)
1597
1.43k
  {
1598
1.43k
    if (pps->saoInfoInPh)
1599
0
    {
1600
0
      WRITE_FLAG(picHeader->saoEnabled[CH_L], "ph_sao_luma_enabled_flag");
1601
0
      if (sps->chromaFormatIdc != CHROMA_400)
1602
0
      {
1603
0
        WRITE_FLAG(picHeader->saoEnabled[CH_C], "ph_sao_chroma_enabled_flag");
1604
0
      }
1605
0
    }
1606
1.43k
  }
1607
1608
  // deblocking filter controls
1609
1.43k
  if (pps->deblockingFilterControlPresent )
1610
0
  {
1611
0
    if(pps->deblockingFilterOverrideEnabled)
1612
0
    {
1613
0
      if (pps->dbfInfoInPh)
1614
0
      {
1615
0
        WRITE_FLAG ( picHeader->deblockingFilterOverride, "ph_deblocking_filter_override_flag" );
1616
 
1617
0
        if(picHeader->deblockingFilterOverride)
1618
0
        {
1619
0
          WRITE_FLAG( picHeader->deblockingFilterDisable, "ph_deblocking_filter_disabled_flag" );
1620
0
          if( !picHeader->deblockingFilterDisable )
1621
0
          {
1622
0
            WRITE_SVLC( picHeader->deblockingFilterBetaOffsetDiv2[COMP_Y], "ph_beta_offset_div2" );
1623
0
            WRITE_SVLC( picHeader->deblockingFilterTcOffsetDiv2[COMP_Y], "ph_tc_offset_div2" );
1624
0
            if( pps->usePPSChromaTool )
1625
0
            {
1626
0
              WRITE_SVLC( picHeader->deblockingFilterBetaOffsetDiv2[COMP_Cb], "ph_cb_beta_offset_div2" );
1627
0
              WRITE_SVLC( picHeader->deblockingFilterTcOffsetDiv2[COMP_Cb], "ph_cb_tc_offset_div2" );
1628
0
              WRITE_SVLC( picHeader->deblockingFilterBetaOffsetDiv2[COMP_Cr], "ph_cr_beta_offset_div2" );
1629
0
              WRITE_SVLC( picHeader->deblockingFilterTcOffsetDiv2[COMP_Cr], "ph_cr_tc_offset_div2" );
1630
0
            }
1631
0
          }
1632
0
        }
1633
0
      }
1634
0
    }
1635
0
  }
1636
1637
  // picture header extension
1638
1.43k
  if(pps->pictureHeaderExtensionPresent)
1639
0
  {
1640
0
    WRITE_UVLC(0,"ph_extension_length");
1641
0
  }
1642
1643
1.43k
  if ( writeRbspTrailingBits )
1644
0
  {
1645
0
    xWriteRbspTrailingBits();
1646
0
  }
1647
1.43k
}
1648
1649
1650
void HLSWriter::codeSliceHeader( const Slice* slice )
1651
1.43k
{
1652
1.43k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Slice ===========\n" );
1653
1654
1.43k
  CodingStructure& cs        = *slice->pic->cs;
1655
1.43k
  const PicHeader *picHeader = cs.picHeader;
1656
1.43k
  const ChromaFormat format  = slice->sps->chromaFormatIdc;
1657
1.43k
  const uint32_t numberValidComponents = getNumberValidComponents(format);
1658
1.43k
  const bool chromaEnabled = isChromaEnabled(format);
1659
1660
1.43k
  WRITE_FLAG(slice->pictureHeaderInSliceHeader, "sh_picture_header_in_slice_header_flag");
1661
1.43k
  if(slice->pictureHeaderInSliceHeader)
1662
1.43k
  {
1663
1.43k
    codePictureHeader(picHeader, false);
1664
1.43k
  }
1665
1666
1.43k
  if (slice->sps->subPicInfoPresent)
1667
0
  {
1668
0
    uint32_t bitsSubPicId;
1669
0
    if (slice->sps->subPicIdMappingExplicitlySignalled)
1670
0
    {
1671
0
      bitsSubPicId = slice->sps->subPicIdLen;
1672
0
    }
1673
0
    else if (slice->pps->subPicIdMappingInPps)
1674
0
    {
1675
0
      bitsSubPicId = slice->pps->subPicIdLen;
1676
0
    }
1677
0
    else
1678
0
    {
1679
0
      bitsSubPicId = ceilLog2(slice->sps->numSubPics);
1680
0
    }
1681
0
    WRITE_CODE(slice->sliceSubPicId, bitsSubPicId, "sh_subpic_id");
1682
0
  }
1683
1684
1.43k
  if (!slice->pps->rectSlice)
1685
0
  {
1686
0
    THROW("no suppport");
1687
0
  }
1688
1.43k
  else
1689
1.43k
  {
1690
    // slice address is the index of the slice within the current sub-picture
1691
1.43k
    uint32_t currSubPicIdx = slice->pps->getSubPicIdxFromSubPicId( slice->sliceSubPicId );
1692
1.43k
    SubPic currSubPic = slice->pps->subPics[currSubPicIdx];
1693
1.43k
    if( currSubPic.numSlicesInSubPic > 1 )
1694
0
    {
1695
0
      int numSlicesInPreviousSubPics = 0;
1696
0
      for(int sp = 0; sp < currSubPicIdx; sp++)
1697
0
      {
1698
0
        numSlicesInPreviousSubPics += slice->pps->subPics[sp].numSlicesInSubPic;
1699
0
      }
1700
0
      int bitsSliceAddress = ceilLog2(currSubPic.numSlicesInSubPic);
1701
0
      WRITE_CODE( slice->sliceMap.sliceID - numSlicesInPreviousSubPics, bitsSliceAddress, "sh_slice_address");
1702
0
    }
1703
1.43k
  }
1704
1705
1.43k
  if (picHeader->picInterSliceAllowed)
1706
0
  {
1707
0
    WRITE_UVLC(slice->sliceType, "sh_slice_type");
1708
0
  }
1709
1.43k
  if (picHeader->gdrOrIrapPic) //th check this
1710
1.43k
  {
1711
1.43k
    WRITE_FLAG(picHeader->noOutputOfPriorPics, "sh_no_output_of_prior_pics_flag");
1712
1.43k
  }
1713
1714
1.43k
  if (!picHeader->picIntraSliceAllowed )
1715
0
  {
1716
0
    CHECK(slice->sliceType == VVENC_I_SLICE, "when pic_intra_slice_allowed_flag = 0, no I_Slice is allowed");
1717
0
  }
1718
1719
1.43k
  if (slice->sps->alfEnabled && !slice->pps->alfInfoInPh)
1720
1.43k
  {
1721
1.43k
    const int alfEnabled = slice->alfEnabled[COMP_Y];
1722
1.43k
    WRITE_FLAG(alfEnabled, "sh_alf_enabled_flag");
1723
1724
1.43k
    if (alfEnabled)
1725
0
    {
1726
0
      WRITE_CODE(slice->numAps, 3, "sh_num_alf_aps_ids_luma");
1727
0
      for (int i = 0; i < slice->numAps; i++)
1728
0
      {
1729
0
        WRITE_CODE(slice->lumaApsId[i], 3, "sh_alf_aps_id_luma");
1730
0
      }
1731
1732
0
      const int alfChromaIdc = slice->alfEnabled[COMP_Cb] + slice->alfEnabled[COMP_Cr] * 2;
1733
0
      if (chromaEnabled)
1734
0
      {
1735
0
        WRITE_FLAG(slice->alfEnabled[COMP_Cb], "sh_alf_cb_enabled_flag");
1736
0
        WRITE_FLAG(slice->alfEnabled[COMP_Cr], "sh_alf_cr_enabled_flag");
1737
0
      }
1738
0
      if (alfChromaIdc)
1739
0
      {
1740
0
        WRITE_CODE(slice->chromaApsId, 3,      "sh_alf_aps_id_chroma");
1741
0
      }
1742
1743
0
      if (slice->sps->ccalfEnabled)
1744
0
      {
1745
0
        WRITE_FLAG(slice->ccAlfCbEnabled,      "sh_cc_alf_cb_enabled_flag");
1746
0
        if( slice->ccAlfCbEnabled )
1747
0
        {
1748
          // write CC ALF Cb APS ID
1749
0
          WRITE_CODE(slice->ccAlfCbApsId, 3,   "sh_cc_alf_cb_aps_id");
1750
0
        }
1751
        // Cr
1752
0
        WRITE_FLAG(slice->ccAlfCrEnabled,      "sh_cc_alf_cr_enabled_flag");
1753
0
        if( slice->ccAlfCrEnabled )
1754
0
        {
1755
          // write CC ALF Cr APS ID
1756
0
          WRITE_CODE(slice->ccAlfCrApsId, 3,   "sh_cc_alf_cr_aps_id");
1757
0
        }
1758
0
      }
1759
0
    }
1760
1.43k
  }
1761
1762
1.43k
  if (picHeader->explicitScalingListEnabled && !slice->pictureHeaderInSliceHeader)
1763
0
  {
1764
0
    WRITE_FLAG(slice->explicitScalingListUsed,  "sh_explicit_scaling_list_used_flag");
1765
0
  }
1766
1767
1.43k
  if(  !slice->pps->rplInfoInPh && (!slice->getIdrPicFlag() || slice->sps->idrRefParamList))
1768
0
  {
1769
0
    int numRPL0 = slice->sps->getNumRPL(0);
1770
    //Write L0 related syntax elements
1771
0
    if (numRPL0 > 0)
1772
0
    {
1773
0
      WRITE_FLAG(slice->rplIdx[0] != -1, "ref_pic_list_sps_flag[0]");
1774
0
    }
1775
0
    if (slice->rplIdx[0] != -1)
1776
0
    {
1777
0
      if (numRPL0 > 1)
1778
0
      {
1779
0
        int numBits = 0;
1780
0
        while ((1 << numBits) < numRPL0)
1781
0
        {
1782
0
          numBits++;
1783
0
        }
1784
0
        WRITE_CODE(slice->rplIdx[0], numBits, "ref_pic_list_idx[0]");
1785
0
      }
1786
0
    }
1787
0
    else
1788
0
    {  //write local RPL0
1789
0
      xCodeRefPicList( slice->rpl[0], slice->sps->longTermRefsPresent, slice->sps->bitsForPOC, !slice->sps->weightPred && !slice->sps->weightedBiPred, -1 );
1790
0
    }
1791
    //Deal POC Msb cycle signalling for LTRP
1792
0
    if (slice->rpl[0]->numberOfLongtermPictures)
1793
0
    {
1794
0
      for (int i = 0; i < slice->rpl[0]->numberOfLongtermPictures + slice->rpl[0]->numberOfShorttermPictures; i++)
1795
0
      {
1796
0
        if (slice->rpl[0]->isLongtermRefPic[i])
1797
0
        {
1798
0
          if (slice->rpl[0]->ltrpInSliceHeader)
1799
0
          {
1800
0
            WRITE_CODE(slice->rpl[0]->refPicIdentifier[i], slice->sps->bitsForPOC, "slice_poc_lsb_lt[listIdx][rplsIdx][j]");
1801
0
          }
1802
0
          WRITE_FLAG(slice->rpl[0]->deltaPocMSBPresent[i], "delta_poc_msb_present_flag[i][j]");
1803
0
          if (slice->rpl[0]->deltaPocMSBPresent[i])
1804
0
          {
1805
0
            WRITE_UVLC(slice->rpl[0]->deltaPocMSBCycleLT[i], "delta_poc_msb_cycle_lt[i][j]");
1806
0
          }
1807
0
        }
1808
0
      }
1809
0
    }
1810
1811
    //Write L1 related syntax elements
1812
0
      if (slice->sps->getNumRPL(1) > 1 && slice->pps->rpl1IdxPresent)
1813
0
      {
1814
0
        WRITE_FLAG(slice->rplIdx[1] != -1 ? 1 : 0, "ref_pic_list_sps_flag[1]");
1815
0
      }
1816
0
      else if (slice->sps->getNumRPL(1) == 0)
1817
0
      {
1818
0
        CHECK(slice->rplIdx[1] != -1, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1819
0
      }
1820
0
      else
1821
0
      {
1822
0
        auto rplsSpsFlag0 = slice->rplIdx[0] != -1 ? 1 : 0;
1823
0
        auto rplsSpsFlag1 = slice->rplIdx[1] != -1 ? 1 : 0;
1824
0
        CHECK(rplsSpsFlag1 != rplsSpsFlag0, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1825
0
      }
1826
1827
0
      if (slice->rplIdx[1] != -1)
1828
0
      {
1829
0
        if (slice->sps->getNumRPL(1) > 1 && slice->pps->rpl1IdxPresent)
1830
0
        {
1831
0
          int numBits = 0;
1832
0
          while ((1 << numBits) < slice->sps->getNumRPL(1))
1833
0
          {
1834
0
            numBits++;
1835
0
          }
1836
0
          WRITE_CODE(slice->rplIdx[1], numBits, "ref_pic_list_idx[1]");
1837
0
        }
1838
0
        else if (slice->sps->getNumRPL(1) == 1)
1839
0
        {
1840
0
          CHECK(slice->rplIdx[1] != 0, "RPL1Idx is not signalled but it is not equal to 0");
1841
0
        }
1842
0
        else
1843
0
        {
1844
0
          CHECK(slice->rplIdx[1] != slice->rplIdx[0], "RPL1Idx is not signalled but it is not the same as RPL0Idx");
1845
0
        }
1846
0
      }
1847
0
      else
1848
0
      {  //write local RPL1
1849
0
        xCodeRefPicList( slice->rpl[1], slice->sps->longTermRefsPresent, slice->sps->bitsForPOC, !(slice->sps->weightPred || slice->sps->weightedBiPred), -1 );
1850
0
      }
1851
      //Deal POC Msb cycle signalling for LTRP
1852
0
      if (slice->rpl[1]->numberOfLongtermPictures)
1853
0
      {
1854
0
        for (int i = 0; i < slice->rpl[1]->numberOfLongtermPictures + slice->rpl[1]->numberOfShorttermPictures; i++)
1855
0
        {
1856
0
          if (slice->rpl[1]->isLongtermRefPic[i])
1857
0
          {
1858
0
            if (slice->rpl[1]->ltrpInSliceHeader)
1859
0
            {
1860
0
              WRITE_CODE(slice->rpl[1]->refPicIdentifier[i], slice->sps->bitsForPOC,
1861
0
                         "slice_poc_lsb_lt[listIdx][rplsIdx][j]");
1862
0
            }
1863
0
            WRITE_FLAG(slice->rpl[1]->deltaPocMSBPresent[i], "delta_poc_msb_present_flag[i][j]");
1864
0
            if (slice->rpl[1]->deltaPocMSBPresent[i])
1865
0
            {
1866
0
              WRITE_UVLC(slice->rpl[1]->deltaPocMSBCycleLT[i], "delta_poc_msb_cycle_lt[i][j]");
1867
0
            }
1868
0
          }
1869
0
        }
1870
0
      }
1871
0
    }
1872
1873
    //check if numrefidxes match the defaults. If not, override
1874
1875
1.43k
    if ((!slice->isIntra() && slice->rpl[0]->getNumRefEntries() > 1) ||
1876
1.43k
        (slice->isInterB() && slice->rpl[1]->getNumRefEntries() > 1) )
1877
0
    {
1878
0
      int defaultL0 = std::min<int>(slice->rpl[0]->getNumRefEntries(), slice->pps->numRefIdxL0DefaultActive);
1879
0
      int defaultL1 = slice->isInterB() ? std::min<int>(slice->rpl[1]->getNumRefEntries(), slice->pps->numRefIdxL1DefaultActive) : 0;
1880
0
      bool overrideFlag = ( slice->numRefIdx[ REF_PIC_LIST_0 ] != defaultL0 || ( slice->isInterB() && slice->numRefIdx[ REF_PIC_LIST_1 ] != defaultL1 ) );
1881
0
      WRITE_FLAG( overrideFlag ? 1 : 0, "num_ref_idx_active_override_flag" );
1882
0
      if( overrideFlag )
1883
0
      {
1884
0
        if(slice->rpl[0]->getNumRefEntries() > 1)
1885
0
        {
1886
0
          WRITE_UVLC( slice->numRefIdx[ REF_PIC_LIST_0 ] - 1, "num_ref_idx_l0_active_minus1" );
1887
0
        }
1888
1889
0
        if( slice->isInterB() && slice->rpl[1]->getNumRefEntries() > 1)
1890
0
        {
1891
0
          WRITE_UVLC( slice->numRefIdx[ REF_PIC_LIST_1 ] - 1, "num_ref_idx_l1_active_minus1" );
1892
0
        }
1893
0
      }
1894
0
    }
1895
1896
1.43k
    if( !slice->isIntra() )
1897
0
    {
1898
0
      if( !slice->isIntra() && slice->pps->cabacInitPresent )
1899
0
      {
1900
0
        const SliceType encCABACTableIdx = slice->encCABACTableIdx;
1901
0
        bool encCabacInitFlag = ( slice->sliceType != encCABACTableIdx && encCABACTableIdx != VVENC_I_SLICE ) ? true : false;
1902
0
        WRITE_FLAG( encCabacInitFlag ? 1 : 0, "sh_cabac_init_flag" );
1903
0
      }
1904
0
    }
1905
1906
1.43k
    if( slice->picHeader->enableTMVP  && !slice->pps->rplInfoInPh)
1907
0
    {
1908
0
      if(!slice->pps->rplInfoInPh)
1909
0
      {
1910
0
        if (slice->sliceType == VVENC_B_SLICE)
1911
0
        {
1912
0
          WRITE_FLAG(slice->colFromL0Flag, "sh_collocated_from_l0_flag");
1913
0
        }
1914
0
      }
1915
1916
0
    if( slice->sliceType != VVENC_I_SLICE &&
1917
0
      ( ( slice->colFromL0Flag == 1 && slice->numRefIdx[ REF_PIC_LIST_0 ] > 1 ) ||
1918
0
        ( slice->colFromL0Flag == 0 && slice->numRefIdx[ REF_PIC_LIST_1 ] > 1 ) ) )
1919
0
    {
1920
0
      WRITE_UVLC( slice->colRefIdx, "sh_collocated_ref_idx" );
1921
0
    }
1922
0
  }
1923
1924
1.43k
  if( ( slice->pps->weightPred && slice->sliceType == VVENC_P_SLICE ) || ( slice->pps->weightedBiPred && slice->sliceType == VVENC_B_SLICE ) )
1925
0
  {
1926
0
    if( !slice->pps->wpInfoInPh )
1927
0
    {
1928
0
      xCodePredWeightTable( slice );
1929
0
    }
1930
0
  }
1931
1932
1.43k
  if (!slice->pps->qpDeltaInfoInPh)
1933
1.43k
  {
1934
1.43k
    WRITE_SVLC(slice->sliceQp - (slice->pps->picInitQPMinus26 + 26), "slice_qp_delta");
1935
1.43k
  }
1936
1.43k
  if (slice->pps->sliceChromaQpFlag)
1937
1.43k
  {
1938
1.43k
    if (numberValidComponents > COMP_Cb)
1939
1.43k
    {
1940
1.43k
      WRITE_SVLC( slice->sliceChromaQpDelta[COMP_Cb], "sh_cb_qp_offset" );
1941
1.43k
    }
1942
1.43k
    if (numberValidComponents > COMP_Cr)
1943
1.43k
    {
1944
1.43k
      WRITE_SVLC( slice->sliceChromaQpDelta[COMP_Cr], "sh_cr_qp_offset" );
1945
1.43k
      if (slice->sps->jointCbCr)
1946
1.43k
      {
1947
1.43k
        WRITE_SVLC( slice->sliceChromaQpDelta[COMP_JOINT_CbCr], "sh_joint_cbcr_qp_offset");
1948
1.43k
      }
1949
1.43k
    }
1950
1.43k
    CHECK(numberValidComponents < COMP_Cr+1, "Too many valid components");
1951
1.43k
  }
1952
1953
1.43k
  if (slice->pps->chromaQpOffsetListLen>0)
1954
0
  {
1955
0
    WRITE_FLAG(slice->chromaQpAdjEnabled, "sh_cu_chroma_qp_offset_enabled_flag");
1956
0
  }
1957
1958
1.43k
  if( slice->sps->saoEnabled && !slice->pps->saoInfoInPh )
1959
1.43k
  {
1960
1.43k
    WRITE_FLAG( slice->saoEnabled[CH_L], "sh_sao_luma_flag" );
1961
1.43k
    if( chromaEnabled )
1962
1.43k
    {
1963
1.43k
      WRITE_FLAG( slice->saoEnabled[CH_C], "sh_sao_chroma_flag" );
1964
1.43k
    }
1965
1.43k
  }
1966
1967
1968
1.43k
  if (slice->pps->deblockingFilterControlPresent && !slice->pps->dbfInfoInPh)
1969
0
  {
1970
0
    if (slice->pps->deblockingFilterOverrideEnabled )
1971
0
    {
1972
0
      WRITE_FLAG(slice->deblockingFilterOverride, "sh_deblocking_params_present_flag");
1973
0
    }
1974
0
    if (slice->deblockingFilterOverride)
1975
0
    {
1976
0
      if (!slice->pps->deblockingFilterDisabled)
1977
0
      {
1978
0
       WRITE_FLAG(slice->deblockingFilterDisable, "sh_deblocking_filter_disabled_flag");
1979
0
      }
1980
0
      if(!slice->deblockingFilterDisable)
1981
0
      {
1982
0
        WRITE_SVLC (slice->deblockingFilterBetaOffsetDiv2[COMP_Y],   "slice_beta_offset_div2");
1983
0
        WRITE_SVLC (slice->deblockingFilterTcOffsetDiv2[COMP_Y],     "slice_tc_offset_div2");
1984
0
        if( slice->pps->usePPSChromaTool )
1985
0
        {
1986
0
          WRITE_SVLC (slice->deblockingFilterBetaOffsetDiv2[COMP_Cb], "slice_cb_beta_offset_div2");
1987
0
          WRITE_SVLC (slice->deblockingFilterTcOffsetDiv2[COMP_Cb],   "slice_cb_tc_offset_div2");
1988
0
          WRITE_SVLC (slice->deblockingFilterBetaOffsetDiv2[COMP_Cr], "slice_cr_beta_offset_div2");
1989
0
          WRITE_SVLC (slice->deblockingFilterTcOffsetDiv2[COMP_Cr],   "slice_cr_tc_offset_div2");
1990
0
        }
1991
0
      }
1992
0
    }
1993
0
  }
1994
1995
  // dependent quantization
1996
1.43k
  if( slice->sps->depQuantEnabled )
1997
1.43k
  {
1998
1.43k
    WRITE_FLAG(slice->depQuantEnabled, "sh_dep_quant_used_flag");
1999
1.43k
  }
2000
2001
  // sign data hiding
2002
1.43k
  if( slice->sps->signDataHidingEnabled && !slice->depQuantEnabled )
2003
0
  {
2004
0
    WRITE_FLAG(slice->signDataHidingEnabled, "sh_sign_data_hiding_used_flag" );
2005
0
  }
2006
1.43k
  if( slice->sps->transformSkip && !slice->depQuantEnabled && !slice->signDataHidingEnabled )
2007
0
  {
2008
0
    WRITE_FLAG(slice->tsResidualCodingDisabled, "sh_ts_residual_coding_disabled_flag");
2009
0
  }
2010
2011
1.43k
  if(slice->pps->sliceHeaderExtensionPresent)
2012
0
  {
2013
0
    WRITE_UVLC(0,"slice_header_extension_length");
2014
0
  }
2015
1.43k
}
2016
2017
void  HLSWriter::codeConstraintInfo  ( const ConstraintInfo* cinfo )
2018
1.43k
{
2019
1.43k
  WRITE_FLAG(cinfo->gciPresent,                         "gci_present_flag");
2020
1.43k
  if (cinfo->gciPresent)
2021
0
  {
2022
0
    WRITE_FLAG(cinfo->intraOnlyConstraintFlag,              "gci_intra_only_constraint_flag");
2023
0
    WRITE_FLAG(cinfo->allLayersIndependentConstraintFlag,   "gci_all_layers_independent_constraint_flag");
2024
0
    WRITE_FLAG(cinfo->onePictureOnlyConstraintFlag,         "gci_one_au_only_constraint_flag");
2025
2026
    /* picture format */
2027
0
    WRITE_CODE(16 - cinfo->maxBitDepthConstraintIdc, 4,     "gci_sixteen_minus_max_bitdepth_constraint_idc");
2028
0
    WRITE_CODE(3 - cinfo->maxChromaFormatConstraintIdc, 2,  "gci_three_minus_max_chroma_format_constraint_idc");
2029
2030
    /* NAL unit type related */
2031
0
    WRITE_FLAG(cinfo->noMixedNaluTypesInPicConstraintFlag,  "gci_no_mixed_nalu_types_in_pic_constraint_flag");
2032
0
    WRITE_FLAG(cinfo->noTrailConstraintFlag,                "gci_no_trail_constraint_flag");
2033
0
    WRITE_FLAG(cinfo->noStsaConstraintFlag,                 "gci_no_stsa_constraint_flag");
2034
0
    WRITE_FLAG(cinfo->noRaslConstraintFlag,                 "gci_no_rasl_constraint_flag");
2035
0
    WRITE_FLAG(cinfo->noRadlConstraintFlag,                 "gci_no_radl_constraint_flag");
2036
0
    WRITE_FLAG(cinfo->noIdrConstraintFlag,                  "gci_no_idr_constraint_flag");
2037
0
    WRITE_FLAG(cinfo->noCraConstraintFlag,                  "gci_no_cra_constraint_flag");
2038
0
    WRITE_FLAG(cinfo->noGdrConstraintFlag,                  "gci_no_gdr_constraint_flag");
2039
0
    WRITE_FLAG(cinfo->noApsConstraintFlag,                  "gci_no_aps_constraint_flag");
2040
0
    WRITE_FLAG(cinfo->noIdrRplConstraintFlag,               "gci_no_idr_rpl_constraint_flag");
2041
2042
    /* tile, slice, subpicture partitioning */
2043
0
    WRITE_FLAG(cinfo->oneTilePerPicConstraintFlag,          "gci_one_tile_per_pic_constraint_flag");
2044
0
    WRITE_FLAG(cinfo->picHeaderInSliceHeaderConstraintFlag, "gci_pic_header_in_slice_header_constraint_flag");
2045
0
    WRITE_FLAG(cinfo->oneSlicePerPicConstraintFlag,         "gci_one_slice_per_pic_constraint_flag");
2046
0
    WRITE_FLAG(cinfo->noRectSliceConstraintFlag,            "gci_no_rectangular_slice_constraint_flag");
2047
0
    WRITE_FLAG(cinfo->oneSlicePerSubpicConstraintFlag,      "gci_one_slice_per_subpic_constraint_flag");
2048
0
    WRITE_FLAG(cinfo->noSubpicInfoConstraintFlag,           "gci_no_subpic_info_constraint_flag");
2049
2050
2051
    /* CTU and block partitioning */
2052
0
    WRITE_CODE(3 - (cinfo->maxLog2CtuSizeConstraintIdc - 5), 2, "gci_three_minus_max_log2_ctu_size_constraint_idc");
2053
0
    WRITE_FLAG(cinfo->noPartitionConstraintsOverrideConstraintFlag, "gci_no_partition_constraints_override_constraint_flag");
2054
0
    WRITE_FLAG(cinfo->noMttConstraintFlag,                  "gci_no_mtt_constraint_flag");
2055
0
    WRITE_FLAG(cinfo->noQtbttDualTreeIntraConstraintFlag,   "gci_no_qtbtt_dual_tree_intra_constraint_flag");
2056
2057
    /* intra */
2058
0
    WRITE_FLAG(cinfo->noPaletteConstraintFlag,              "gci_no_palette_constraint_flag");
2059
0
    WRITE_FLAG(cinfo->noIbcConstraintFlag,                  "gci_no_ibc_constraint_flag");
2060
0
    WRITE_FLAG(cinfo->noIspConstraintFlag,                  "gci_no_isp_constraint_flag");
2061
0
    WRITE_FLAG(cinfo->noMrlConstraintFlag,                  "gci_no_mrl_constraint_flag");
2062
0
    WRITE_FLAG(cinfo->noMipConstraintFlag,                  "gci_no_mip_constraint_flag");
2063
0
    WRITE_FLAG(cinfo->noCclmConstraintFlag,                 "gci_no_cclm_constraint_flag");
2064
2065
    /* inter */
2066
0
    WRITE_FLAG(cinfo->noRprConstraintFlag,                  "gci_no_ref_pic_resampling_constraint_flag");
2067
0
    WRITE_FLAG(cinfo->noResChangeInClvsConstraintFlag,      "gci_no_res_change_in_clvs_constraint_flag");
2068
0
    WRITE_FLAG(cinfo->noWeightedPredictionConstraintFlag,   "gci_no_weighted_prediction_constraint_flag");
2069
0
    WRITE_FLAG(cinfo->noRefWraparoundConstraintFlag,        "gci_no_ref_wraparound_constraint_flag");
2070
0
    WRITE_FLAG(cinfo->noTemporalMvpConstraintFlag,          "gci_no_temporal_mvp_constraint_flag");
2071
0
    WRITE_FLAG(cinfo->noSbtmvpConstraintFlag,               "gci_no_sbtmvp_constraint_flag");
2072
0
    WRITE_FLAG(cinfo->noAmvrConstraintFlag,                 "gci_no_amvr_constraint_flag");
2073
0
    WRITE_FLAG(cinfo->noBdofConstraintFlag,                 "gci_no_bdof_constraint_flag");
2074
0
    WRITE_FLAG(cinfo->noSmvdConstraintFlag,                 "gci_no_smvd_constraint_flag");
2075
0
    WRITE_FLAG(cinfo->noDmvrConstraintFlag,                 "gci_no_dmvr_constraint_flag");
2076
0
    WRITE_FLAG(cinfo->noMmvdConstraintFlag,                 "gci_no_mmvd_constraint_flag");
2077
0
    WRITE_FLAG(cinfo->noAffineMotionConstraintFlag,         "gci_no_affine_motion_constraint_flag");
2078
0
    WRITE_FLAG(cinfo->noProfConstraintFlag,                 "gci_no_prof_constraint_flag");
2079
0
    WRITE_FLAG(cinfo->noBcwConstraintFlag,                  "gci_no_bcw_constraint_flag");
2080
0
    WRITE_FLAG(cinfo->noCiipConstraintFlag,                 "gci_no_ciip_constraint_flag");
2081
0
    WRITE_FLAG(cinfo->noGeoConstraintFlag,                  "gci_no_gpm_constraint_flag");
2082
2083
    /* transform, quantization, residual */
2084
0
    WRITE_FLAG(cinfo->noLumaTransformSize64ConstraintFlag,  "gci_no_luma_transform_size_64_constraint_flag");
2085
0
    WRITE_FLAG(cinfo->noTransformSkipConstraintFlag,        "gci_no_transform_skip_constraint_flag");
2086
0
    WRITE_FLAG(cinfo->noBDPCMConstraintFlag,                "gci_no_bdpcm_constraint_flag");
2087
0
    WRITE_FLAG(cinfo->noMtsConstraintFlag,                  "gci_no_mts_constraint_flag");
2088
0
    WRITE_FLAG(cinfo->noLfnstConstraintFlag,                "gci_no_lfnst_constraint_flag");
2089
0
    WRITE_FLAG(cinfo->noJointCbCrConstraintFlag,            "gci_no_joint_cbcr_constraint_flag");
2090
0
    WRITE_FLAG(cinfo->noSbtConstraintFlag,                  "gci_no_sbt_constraint_flag");
2091
0
    WRITE_FLAG(cinfo->noActConstraintFlag,                  "gci_no_act_constraint_flag");
2092
0
    WRITE_FLAG(cinfo->noExplicitScaleListConstraintFlag,    "gci_no_explicit_scaling_list_constraint_flag");
2093
0
    WRITE_FLAG(cinfo->noDepQuantConstraintFlag,             "gci_no_dep_quant_constraint_flag");
2094
0
    WRITE_FLAG(cinfo->noSignDataHidingConstraintFlag,       "gci_no_sign_data_hiding_constraint_flag");
2095
0
    WRITE_FLAG(cinfo->noQpDeltaConstraintFlag,              "gci_no_qp_delta_constraint_flag");
2096
0
    WRITE_FLAG(cinfo->noChromaQpOffsetConstraintFlag,       "gci_no_chroma_qp_offset_constraint_flag");
2097
2098
    /* loop filter */
2099
0
    WRITE_FLAG(cinfo->noSaoConstraintFlag,                  "gci_no_sao_constraint_flag");
2100
0
    WRITE_FLAG(cinfo->noAlfConstraintFlag,                  "gci_no_alf_constraint_flag");
2101
0
    WRITE_FLAG(cinfo->noCCAlfConstraintFlag,                "gci_no_ccalf_constraint_flag");
2102
0
    WRITE_FLAG(cinfo->noLmcsConstraintFlag,                 "gci_no_lmcs_constraint_flag");
2103
0
    WRITE_FLAG(cinfo->noLadfConstraintFlag,                 "gci_no_ladf_constraint_flag");
2104
0
    WRITE_FLAG(cinfo->noVirtualBoundaryConstraintFlag,      "gci_no_virtual_boundaries_constraint_flag");
2105
2106
    //The value of gci_num_reserved_bits shall be equal to 0 in bitstreams conforming to this version of this Specification.
2107
    //Other values of gci_num_reserved_bits are reserved for future use by ITU-T | ISO/IEC.
2108
0
    WRITE_CODE(0, 8,                                        "gci_num_reserved_bits");
2109
0
  }
2110
2111
8.59k
  while (!isByteAligned())
2112
7.16k
  {
2113
7.16k
    WRITE_FLAG(0, "gci_alignment_zero_bit");
2114
7.16k
  }
2115
1.43k
}
2116
2117
2118
void  HLSWriter::codeProfileTierLevel    ( const ProfileTierLevel* ptl, bool profileTierPresent, int maxNumSubLayersMinus1 )
2119
1.43k
{
2120
1.43k
  if(profileTierPresent)
2121
1.43k
  {
2122
1.43k
    WRITE_CODE( (uint32_t)ptl->profileIdc, 7 ,        "general_profile_idc"                     );
2123
1.43k
    WRITE_FLAG( ptl->tierFlag==vvencTier::VVENC_TIER_HIGH,           "general_tier_flag"                       );
2124
1.43k
  }
2125
2126
1.43k
  WRITE_CODE( (uint32_t)ptl->levelIdc, 8 ,            "general_level_idc");
2127
1.43k
  WRITE_FLAG( ptl->frameOnlyConstraintFlag,           "ptl_frame_only_constraint_flag" );
2128
1.43k
  WRITE_FLAG( ptl->multiLayerEnabledFlag,             "ptl_multilayer_enabled_flag"    );
2129
1.43k
  if(profileTierPresent)
2130
1.43k
  {
2131
1.43k
    codeConstraintInfo( &ptl->constraintInfo );
2132
1.43k
  }
2133
2134
8.59k
  for (int i = maxNumSubLayersMinus1 - 1; i >= 0; i--)
2135
7.16k
  {
2136
7.16k
    WRITE_FLAG( ptl->subLayerLevelPresent[i],         "sub_layer_level_present_flag[i]" );
2137
7.16k
  }
2138
2139
5.72k
  while (!isByteAligned())
2140
4.29k
  {
2141
4.29k
    WRITE_FLAG(0,                                     "ptl_reserved_zero_bit");
2142
4.29k
  }
2143
2144
8.59k
  for (int i = maxNumSubLayersMinus1 - 1; i >= 0; i--)
2145
7.16k
  {
2146
7.16k
    if( ptl->subLayerLevelPresent[i] )
2147
0
    {
2148
0
      WRITE_CODE( (uint32_t)ptl->subLayerLevelIdc[i], 8, "sub_layer_level_idc[i]" );
2149
0
    }
2150
7.16k
  }
2151
2152
1.43k
  if (profileTierPresent)
2153
1.43k
  {
2154
1.43k
    WRITE_CODE(ptl->numSubProfile, 8, "ptl_num_sub_profiles");
2155
1.43k
    for (int i = 0; i < ptl->numSubProfile; i++)
2156
0
    {
2157
0
      WRITE_CODE(ptl->subProfileIdc[i], 32, "general_sub_profile_idc[i]");
2158
0
    }
2159
1.43k
  }
2160
1.43k
}
2161
2162
2163
/**
2164
* Write tiles and wavefront substreams sizes for the slice header (entry points).
2165
*
2166
* \param pSlice Slice structure that contains the substream size information.
2167
*/
2168
void  HLSWriter::codeTilesWPPEntryPoint( Slice* pSlice )
2169
1.43k
{
2170
1.43k
  int numEntryPoints = pSlice->getNumEntryPoints( *pSlice->sps, *pSlice->pps );
2171
1.43k
  if( numEntryPoints == 0 )
2172
1.43k
  {
2173
1.43k
    return;
2174
1.43k
  }
2175
2176
0
  uint32_t maxOffset = 0;
2177
0
  for(int idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2178
0
  {
2179
0
    uint32_t offset=pSlice->getSubstreamSize(idx);
2180
0
    if ( offset > maxOffset )
2181
0
    {
2182
0
      maxOffset = offset;
2183
0
    }
2184
0
  }
2185
2186
  // Determine number of bits "offsetLenMinus1+1" required for entry point information
2187
0
  uint32_t offsetLenMinus1 = 0;
2188
0
  while (maxOffset >= (1u << (offsetLenMinus1 + 1)))
2189
0
  {
2190
0
    offsetLenMinus1++;
2191
0
    CHECK(offsetLenMinus1 + 1 >= 32, "Invalid offset length minus 1");
2192
0
  }
2193
2194
0
  if (pSlice->getNumberOfSubstreamSizes()>0)
2195
0
  {
2196
0
    WRITE_UVLC(offsetLenMinus1, "sh_entry_offset_len_minus1");
2197
2198
0
    for (uint32_t idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2199
0
    {
2200
0
      WRITE_CODE(pSlice->getSubstreamSize(idx)-1, offsetLenMinus1+1, "sh_entry_point_offset_minus1");
2201
0
    }
2202
0
  }
2203
0
}
2204
2205
2206
// ====================================================================================================================
2207
// Protected member functions
2208
// ====================================================================================================================
2209
2210
//! Code weighted prediction tables
2211
void HLSWriter::xCodePredWeightTable( const Slice* slice )
2212
0
{
2213
0
  WPScalingParam      *wp;
2214
0
  const ChromaFormat  format                = slice->sps->chromaFormatIdc;
2215
0
  const uint32_t      numberValidComponents = getNumberValidComponents(format);
2216
0
  const bool          bChroma               = isChromaEnabled(format);
2217
0
  const int           iNbRef                = (slice->sliceType == VVENC_B_SLICE ) ? (2) : (1);
2218
0
  bool                bDenomCoded           = false;
2219
0
  uint32_t            uiTotalSignalledWeightFlags = 0;
2220
2221
0
  if ( (slice->sliceType==VVENC_P_SLICE && slice->pps->weightPred) || (slice->sliceType==VVENC_B_SLICE && slice->pps->weightedBiPred) )
2222
0
  {
2223
0
    for ( int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
2224
0
    {
2225
0
      RefPicList  refPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2226
2227
      // NOTE: wp[].log2WeightDenom and wp[].presentFlag are actually per-channel-type settings.
2228
2229
0
      for ( int iRefIdx=0 ; iRefIdx<slice->numRefIdx[ refPicList ] ; iRefIdx++ )
2230
0
      {
2231
0
        slice->getWpScaling(refPicList, iRefIdx, wp);
2232
0
        if ( !bDenomCoded )
2233
0
        {
2234
0
          int iDeltaDenom;
2235
0
          WRITE_UVLC( wp[COMP_Y].log2WeightDenom, "luma_log2_weight_denom" );
2236
2237
0
          if( bChroma )
2238
0
          {
2239
0
            CHECK( wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported" );
2240
0
            iDeltaDenom = (wp[COMP_Cb].log2WeightDenom - wp[COMP_Y].log2WeightDenom);
2241
0
            WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
2242
0
          }
2243
0
          bDenomCoded = true;
2244
0
        }
2245
0
        WRITE_FLAG( wp[COMP_Y].presentFlag, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
2246
0
        uiTotalSignalledWeightFlags += wp[COMP_Y].presentFlag;
2247
0
      }
2248
0
      if (bChroma)
2249
0
      {
2250
0
        for ( int iRefIdx=0 ; iRefIdx<slice->numRefIdx[ refPicList ] ; iRefIdx++ )
2251
0
        {
2252
0
          slice->getWpScaling( refPicList, iRefIdx, wp );
2253
0
          CHECK( wp[COMP_Cb].presentFlag != wp[COMP_Cr].presentFlag, "Inconsistent settings for chroma channels" );
2254
0
          WRITE_FLAG( wp[COMP_Cb].presentFlag, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
2255
0
          uiTotalSignalledWeightFlags += 2*wp[COMP_Cb].presentFlag;
2256
0
        }
2257
0
      }
2258
2259
0
      for ( int iRefIdx=0 ; iRefIdx<slice->numRefIdx[ refPicList ] ; iRefIdx++ )
2260
0
      {
2261
0
        slice->getWpScaling(refPicList, iRefIdx, wp);
2262
0
        if ( wp[COMP_Y].presentFlag )
2263
0
        {
2264
0
          int iDeltaWeight = (wp[COMP_Y].iWeight - (1<<wp[COMP_Y].log2WeightDenom));
2265
0
          WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
2266
0
          WRITE_SVLC( wp[COMP_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
2267
0
        }
2268
2269
0
        if ( bChroma )
2270
0
        {
2271
0
          if ( wp[COMP_Cb].presentFlag )
2272
0
          {
2273
0
            for ( int j = COMP_Cb ; j < numberValidComponents ; j++ )
2274
0
            {
2275
0
              CHECK(wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported");
2276
0
              int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMP_Cb].log2WeightDenom));
2277
0
              WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
2278
2279
0
              int range=128;
2280
0
              int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].log2WeightDenom) ) );
2281
0
              int iDeltaChroma = (wp[j].iOffset - pred);
2282
0
              WRITE_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
2283
0
            }
2284
0
          }
2285
0
        }
2286
0
      }
2287
0
    }
2288
0
    CHECK(uiTotalSignalledWeightFlags>24, "Too many signalled weight flags");
2289
0
  }
2290
0
}
2291
2292
2293
void HLSWriter::xCodePredWeightTable( const PicHeader *picHeader, const PPS *pps, const SPS *sps )
2294
0
{
2295
0
  WPScalingParam  *wp;
2296
0
  const ChromaFormat  format                = sps->chromaFormatIdc;
2297
0
  const uint32_t      numberValidComponents = getNumberValidComponents(format);
2298
0
  const bool          bChroma               = isChromaEnabled(format);
2299
0
  bool                bDenomCoded           = false;
2300
0
  uint32_t            uiTotalSignalledWeightFlags = 0;
2301
0
  uint32_t            numLxWeights                = picHeader->numL0Weights;
2302
0
  bool                moreSyntaxToBeParsed        = true;
2303
0
  for (int iNumRef = 0; iNumRef < NUM_REF_PIC_LIST_01 && moreSyntaxToBeParsed; iNumRef++)   // loop over l0 and l1 syntax elements
2304
0
  {
2305
0
    RefPicList  refPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2306
2307
    // NOTE: wp[].log2WeightDenom and wp[].presentFlag are actually per-channel-type settings.
2308
2309
0
    for ( int iRefIdx=0 ; iRefIdx<numLxWeights ; iRefIdx++ )
2310
0
    {
2311
0
      picHeader->getWpScaling(refPicList, iRefIdx, wp);
2312
0
      if ( !bDenomCoded )
2313
0
      {
2314
0
        int iDeltaDenom;
2315
0
        WRITE_UVLC( wp[COMP_Y].log2WeightDenom, "luma_log2_weight_denom" );
2316
2317
0
        if( bChroma )
2318
0
        {
2319
0
          CHECK( wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported" );
2320
0
          iDeltaDenom = (wp[COMP_Cb].log2WeightDenom - wp[COMP_Y].log2WeightDenom);
2321
0
          WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
2322
0
        }
2323
0
        bDenomCoded = true;
2324
0
      }
2325
0
      WRITE_FLAG( wp[COMP_Y].presentFlag, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
2326
0
      uiTotalSignalledWeightFlags += wp[COMP_Y].presentFlag;
2327
0
    }
2328
0
    if (bChroma)
2329
0
    {
2330
0
      for ( int iRefIdx=0 ; iRefIdx<numLxWeights; iRefIdx++ )
2331
0
      {
2332
0
        picHeader->getWpScaling( refPicList, iRefIdx, wp );
2333
0
        CHECK( wp[COMP_Cb].presentFlag != wp[COMP_Cr].presentFlag, "Inconsistent settings for chroma channels" );
2334
0
        WRITE_FLAG( wp[COMP_Cb].presentFlag, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
2335
0
        uiTotalSignalledWeightFlags += 2*wp[COMP_Cb].presentFlag;
2336
0
      }
2337
0
    }
2338
2339
0
    for ( int iRefIdx=0 ; iRefIdx<numLxWeights; iRefIdx++ )
2340
0
    {
2341
0
      picHeader->getWpScaling(refPicList, iRefIdx, wp);
2342
0
      if ( wp[COMP_Y].presentFlag )
2343
0
      {
2344
0
        int iDeltaWeight = (wp[COMP_Y].iWeight - (1<<wp[COMP_Y].log2WeightDenom));
2345
0
        WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
2346
0
        WRITE_SVLC( wp[COMP_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
2347
0
      }
2348
2349
0
      if ( bChroma )
2350
0
      {
2351
0
        if ( wp[COMP_Cb].presentFlag )
2352
0
        {
2353
0
          for ( int j = COMP_Cb ; j < numberValidComponents ; j++ )
2354
0
          {
2355
0
            CHECK(wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported");
2356
0
            int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMP_Cb].log2WeightDenom));
2357
0
            WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
2358
2359
0
            int range=128;
2360
0
            int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].log2WeightDenom) ) );
2361
0
            int iDeltaChroma = (wp[j].iOffset - pred);
2362
0
            WRITE_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
2363
0
          }
2364
0
        }
2365
0
      }
2366
0
    }
2367
0
    if (iNumRef == 0 )
2368
0
    {
2369
0
      numLxWeights = picHeader->numL1Weights;
2370
0
      if (pps->weightedBiPred == 0) 
2371
0
      {
2372
0
        numLxWeights = 0;
2373
0
      }
2374
0
      else if (picHeader->pRPL[1]->getNumRefEntries() > 0)
2375
0
      {
2376
0
        WRITE_UVLC(numLxWeights, "num_l1_weights");
2377
0
      }
2378
0
      moreSyntaxToBeParsed = (numLxWeights == 0) ? false : true;
2379
0
    }
2380
0
  }
2381
0
  CHECK(uiTotalSignalledWeightFlags>24, "Too many signalled weight flags");
2382
0
}
2383
2384
void HLSWriter::alfFilter( const AlfParam& alfParam, const bool isChroma, const int altIdx )
2385
0
{
2386
0
  AlfFilterShape alfShape(isChroma ? 5 : 7);
2387
0
  const short* coeff = isChroma ? alfParam.chromaCoeff[altIdx] : alfParam.lumaCoeff;
2388
0
  const short* clipp = isChroma ? alfParam.chromaClipp[altIdx] : alfParam.lumaClipp;
2389
0
  const int numFilters = isChroma ? 1 : alfParam.numLumaFilters;
2390
2391
  // vlc for all
2392
2393
  // Filter coefficients
2394
0
  for( int ind = 0; ind < numFilters; ++ind )
2395
0
  {
2396
0
    for( int i = 0; i < alfShape.numCoeff - 1; i++ )
2397
0
    {
2398
0
      WRITE_UVLC( abs(coeff[ ind* MAX_NUM_ALF_LUMA_COEFF + i ]), isChroma ? "alf_chroma_coeff_abs" : "alf_luma_coeff_abs" ); //alf_coeff_chroma[i], alf_coeff_luma_delta[i][j]
2399
0
      if( abs( coeff[ ind* MAX_NUM_ALF_LUMA_COEFF + i ] ) != 0 )
2400
0
      {
2401
0
        WRITE_FLAG( ( coeff[ ind* MAX_NUM_ALF_LUMA_COEFF + i ] < 0 ) ? 1 : 0, isChroma ? "alf_chroma_coeff_sign" : "alf_luma_coeff_sign" );
2402
0
      }
2403
0
    }
2404
0
  }
2405
2406
  // Clipping values coding
2407
0
  if( alfParam.nonLinearFlag[isChroma] )
2408
0
  {
2409
0
    for (int ind = 0; ind < numFilters; ++ind)
2410
0
    {
2411
0
      for (int i = 0; i < alfShape.numCoeff - 1; i++)
2412
0
      {
2413
0
        WRITE_CODE(clipp[ind* MAX_NUM_ALF_LUMA_COEFF + i], 2, isChroma ? "alf_chroma_clip_idx" : "alf_luma_clip_idx");
2414
0
      }
2415
0
    }
2416
0
  }
2417
0
}
2418
2419
} // namespace vvenc
2420
2421
//! \}
2422