Coverage Report

Created: 2026-08-13 07:23

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
19.2k
{
135
19.2k
  CHECK( uiLength == 0, "Code of length '0' not supported" );
136
19.2k
  m_pcBitIf->write( uiCode, uiLength );
137
19.2k
}
138
139
void VLCWriter::xWriteUvlc     ( uint32_t uiCode )
140
259k
{
141
259k
  uint32_t uiLength = 1;
142
259k
  uint32_t uiTemp = ++uiCode;
143
144
259k
  CHECK( !uiTemp, "Integer overflow" );
145
146
696k
  while( 1 != uiTemp )
147
437k
  {
148
437k
    uiTemp >>= 1;
149
437k
    uiLength += 2;
150
437k
  }
151
  // Take care of cases where uiLength > 32
152
259k
  m_pcBitIf->write( 0, uiLength >> 1);
153
259k
  m_pcBitIf->write( uiCode, (uiLength+1) >> 1);
154
259k
}
155
156
void VLCWriter::xWriteSvlc     ( int iCode )
157
10.8k
{
158
10.8k
  uint32_t uiCode = uint32_t( iCode <= 0 ? (-iCode)<<1 : (iCode<<1)-1);
159
10.8k
  xWriteUvlc( uiCode );
160
10.8k
}
161
162
void VLCWriter::xWriteFlag( bool flag )
163
295k
{
164
295k
  m_pcBitIf->write( flag?1:0, 1 );
165
295k
}
166
167
void VLCWriter::xWriteRbspTrailingBits()
168
2.40k
{
169
2.40k
  WRITE_FLAG( 1, "rbsp_stop_one_bit");
170
2.40k
  int cnt = 0;
171
12.3k
  while (m_pcBitIf->getNumBitsUntilByteAligned())
172
9.98k
  {
173
9.98k
    WRITE_FLAG( 0, "rbsp_alignment_zero_bit");
174
9.98k
    cnt++;
175
9.98k
  }
176
2.40k
  CHECK(cnt>=8, "More than '8' alignment bytes read");
177
2.40k
}
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
57.6k
{
191
57.6k
  uint32_t numRefPic = rpl->numberOfShorttermPictures + rpl->numberOfLongtermPictures + rpl->numberOfInterLayerPictures;
192
57.6k
  WRITE_UVLC(numRefPic, "num_ref_entries[ listIdx ][ rplsIdx ]");
193
194
57.6k
  if (isLongTermPresent && numRefPic > 0 && rplIdx != -1)
195
0
  {
196
0
    WRITE_FLAG(rpl->ltrpInSliceHeader, "ltrp_in_slice_header_flag[ listIdx ][ rplsIdx ]");
197
0
  }
198
57.6k
  int prevDelta = MAX_INT;
199
57.6k
  int deltaValue = 0;
200
57.6k
  bool firstSTRP = true;
201
198k
  for (int ii = 0; ii < numRefPic; ii++)
202
140k
  {
203
140k
    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
140k
    if( !rpl->isInterLayerRefPic[ii] )
215
140k
    {
216
140k
      if (isLongTermPresent)
217
0
      {
218
0
        WRITE_FLAG(!rpl->isLongtermRefPic[ii], "st_ref_pic_flag[ listIdx ][ rplsIdx ][ i ]");
219
0
      }
220
140k
      if (!rpl->isLongtermRefPic[ii])
221
140k
      {
222
140k
        if (firstSTRP)
223
57.6k
        {
224
57.6k
          firstSTRP = false;
225
57.6k
          deltaValue = prevDelta = rpl->refPicIdentifier[ii];
226
57.6k
        }
227
82.8k
        else
228
82.8k
        {
229
82.8k
          deltaValue = rpl->refPicIdentifier[ii] - prevDelta;
230
82.8k
          prevDelta = rpl->refPicIdentifier[ii];
231
82.8k
        }
232
140k
        unsigned int absDeltaValue = (deltaValue < 0) ? 0 - deltaValue : deltaValue;
233
140k
        if( isForbiddenZeroDeltaPoc || ii == 0 )
234
140k
        {
235
140k
          CHECK( !absDeltaValue, "Zero delta POC is not used without WP" );
236
140k
          WRITE_UVLC( absDeltaValue - 1, "abs_delta_poc_st[ listIdx ][ rplsIdx ][ i ]" );
237
140k
        }
238
0
        else
239
0
        WRITE_UVLC(absDeltaValue, "abs_delta_poc_st[ listIdx ][ rplsIdx ][ i ]");
240
140k
        if (absDeltaValue > 0)
241
140k
        {
242
140k
          WRITE_FLAG((deltaValue < 0), "strp_entry_sign_flag[ listIdx ][ rplsIdx ][ i ]");  //0  means negative delta POC : 1 means positive
243
140k
        }
244
140k
      }
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
140k
    }
250
140k
  }
251
57.6k
}
252
253
void HLSWriter::codePPS( const PPS* pcPPS, const SPS* pcSPS )
254
1.20k
{
255
1.20k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Picture Parameter Set  ===========\n" );
256
257
1.20k
  WRITE_CODE( pcPPS->ppsId, 6,                        "pps_pic_parameter_set_id" );
258
1.20k
  WRITE_CODE( pcPPS->spsId, 4,                        "pps_seq_parameter_set_id" );
259
260
1.20k
  WRITE_FLAG( pcPPS->mixedNaluTypesInPic,             "pps_mixed_nalu_types_in_pic_flag" );
261
262
1.20k
  WRITE_UVLC( pcPPS->picWidthInLumaSamples,           "pic_width_in_luma_samples" );
263
1.20k
  WRITE_UVLC( pcPPS->picHeightInLumaSamples,          "pic_height_in_luma_samples" );
264
265
1.20k
  if( pcPPS->picWidthInLumaSamples == pcSPS->maxPicWidthInLumaSamples && pcPPS->picHeightInLumaSamples == pcSPS->maxPicHeightInLumaSamples )
266
1.20k
  {
267
1.20k
    WRITE_FLAG( 0,                                    "pps_conformance_window_flag" );
268
1.20k
  }
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.20k
  const Window& scWnd = pcPPS->scalingWindow;
283
1.20k
  WRITE_FLAG( scWnd.enabledFlag,                      "pps_scaling_window_flag" );
284
1.20k
  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.20k
  WRITE_FLAG( pcPPS->outputFlagPresent,               "pps_output_flag_present_flag" );
293
1.20k
  WRITE_FLAG( pcPPS->noPicPartition,                  "pps_no_pic_partition_flag" );
294
1.20k
  WRITE_FLAG( pcPPS->subPicIdMappingInPps,            "pps_subpic_id_mapping_in_pps_flag" );
295
1.20k
  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.20k
  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.20k
  WRITE_FLAG( pcPPS->cabacInitPresent,                "pps_cabac_init_present_flag" );
347
1.20k
  WRITE_UVLC( pcPPS->numRefIdxL0DefaultActive-1,      "pps_num_ref_idx_l0_default_active_minus1");
348
1.20k
  WRITE_UVLC( pcPPS->numRefIdxL1DefaultActive-1,      "pps_num_ref_idx_l1_default_active_minus1");
349
1.20k
  WRITE_FLAG( pcPPS->rpl1IdxPresent,                  "pps_rpl1_idx_present_flag");
350
351
1.20k
  WRITE_FLAG( pcPPS->weightPred,                      "pps_weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
352
1.20k
  WRITE_FLAG( pcPPS->weightedBiPred,                  "pps_weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
353
1.20k
  WRITE_FLAG( pcPPS->wrapAroundEnabled,               "pps_ref_wraparound_enabled_flag" );
354
1.20k
  if( pcPPS->wrapAroundEnabled )
355
0
  {
356
0
    WRITE_UVLC(pcPPS->picWidthMinusWrapAroundOffset,  "pps_pic_width_minus_wraparound_offset");
357
0
  }
358
359
1.20k
  WRITE_SVLC( pcPPS->picInitQPMinus26,                "pps_init_qp_minus26");
360
1.20k
  WRITE_FLAG( pcPPS->useDQP,                          "pps_cu_qp_delta_enabled_flag" );
361
1.20k
  WRITE_FLAG (pcPPS->usePPSChromaTool,                "pps_chroma_tool_offsets_present_flag");
362
1.20k
  if (pcPPS->usePPSChromaTool)
363
1.20k
  {
364
1.20k
    WRITE_SVLC( pcPPS->chromaQpOffset[COMP_Cb],       "pps_cb_qp_offset" );
365
1.20k
    WRITE_SVLC( pcPPS->chromaQpOffset[COMP_Cr],       "pps_cr_qp_offset" );
366
1.20k
    WRITE_FLAG( pcPPS->jointCbCrQpOffsetPresent,      "pps_joint_cbcr_qp_offset_present_flag");
367
1.20k
    if (pcPPS->jointCbCrQpOffsetPresent)
368
1.20k
    {
369
1.20k
      WRITE_SVLC(pcPPS->chromaQpOffset[COMP_JOINT_CbCr],"pps_joint_cbcr_qp_offset_value");
370
1.20k
    }
371
372
1.20k
    WRITE_FLAG( pcPPS->sliceChromaQpFlag,               "pps_slice_chroma_qp_offsets_present_flag" );
373
374
1.20k
    bool cuChromaQpOffsetEnabled = pcPPS->chromaQpOffsetListLen>0;
375
1.20k
    WRITE_FLAG(cuChromaQpOffsetEnabled,                 "pps_cu_chroma_qp_offset_list_enabled_flag" );
376
1.20k
    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.20k
  }
391
1.20k
  WRITE_FLAG( pcPPS->deblockingFilterControlPresent,    "pps_deblocking_filter_control_present_flag");
392
1.20k
  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.20k
  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.20k
  WRITE_FLAG( pcPPS->pictureHeaderExtensionPresent,       "pps_picture_header_extension_present_flag");
427
1.20k
  WRITE_FLAG( pcPPS->sliceHeaderExtensionPresent,         "pps_slice_header_extension_present_flag");
428
429
1.20k
  WRITE_FLAG( false,                                      "pps_extension_present_flag" );
430
431
1.20k
  xWriteRbspTrailingBits();
432
1.20k
}
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.20k
{
604
1.20k
  WRITE_CODE(hrd->numUnitsInTick, 32,                   "num_units_in_tick");
605
1.20k
  WRITE_CODE(hrd->timeScale, 32,                        "time_scale");
606
1.20k
  WRITE_FLAG(hrd->generalNalHrdParamsPresent,           "general_nal_hrd_parameters_present_flag");
607
1.20k
  WRITE_FLAG(hrd->generalVclHrdParamsPresent,           "general_vcl_hrd_parameters_present_flag");
608
1.20k
  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.20k
}
625
626
void HLSWriter::codeOlsHrdParameters(const GeneralHrdParams * generalHrd, const OlsHrdParams *olsHrd, const uint32_t firstSubLayer, const uint32_t maxNumSubLayersMinus1)
627
1.20k
{
628
2.40k
  for( int i = firstSubLayer; i <= maxNumSubLayersMinus1; i ++ )
629
1.20k
  {
630
1.20k
    const OlsHrdParams *hrd = &(olsHrd[i]);
631
1.20k
    WRITE_FLAG(hrd->fixedPicRateGeneralFlag,      "fixed_pic_rate_general_flag");
632
633
1.20k
    if (!hrd->fixedPicRateGeneralFlag)
634
0
    {
635
0
      WRITE_FLAG(hrd->fixedPicRateWithinCvsFlag,  "fixed_pic_rate_within_cvs_flag");
636
0
    }
637
1.20k
    if (hrd->fixedPicRateWithinCvsFlag)
638
1.20k
    {
639
1.20k
      WRITE_UVLC(hrd->elementDurationInTcMinus1,  "elemental_duration_in_tc_minus1");
640
1.20k
    }
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
3.60k
    for( int nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
647
2.40k
    {
648
2.40k
      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.40k
    }
663
1.20k
  }
664
1.20k
}
665
666
void HLSWriter::dpb_parameters(int maxSubLayersMinus1, bool subLayerInfoFlag, const SPS *pcSPS)
667
1.20k
{
668
2.40k
  for (uint32_t i = (subLayerInfoFlag ? 0 : maxSubLayersMinus1); i <= maxSubLayersMinus1; i++)
669
1.20k
  {
670
1.20k
    WRITE_UVLC(pcSPS->maxDecPicBuffering[i] - 1,          "dpb_max_dec_pic_buffering_minus1[i]");
671
1.20k
    WRITE_UVLC(pcSPS->numReorderPics[i],                  "dpb_max_num_reorder_pics[i]");
672
1.20k
    WRITE_UVLC(pcSPS->maxLatencyIncreasePlus1[i],         "dpb_max_latency_increase_plus1[i]");
673
1.20k
  }
674
1.20k
}
675
676
void HLSWriter::codeSPS( const SPS* pcSPS )
677
1.20k
{
678
1.20k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Sequence Parameter Set  ===========\n" );
679
680
1.20k
  WRITE_CODE( pcSPS->spsId, 4,                            "sps_seq_parameter_set_id" );
681
1.20k
  WRITE_CODE( pcSPS->vpsId, 4,                            "sps_video_parameter_set_id" );
682
1.20k
  CHECK(pcSPS->maxTLayers == 0, "Maximum number of temporal sub-layers is '0'");
683
684
1.20k
  WRITE_CODE(pcSPS->maxTLayers - 1, 3,                    "sps_max_sub_layers_minus1");
685
1.20k
  WRITE_CODE( int(pcSPS->chromaFormatIdc), 2,             "sps_chroma_format_idc" );
686
1.20k
  WRITE_CODE(floorLog2(pcSPS->CTUSize) - 5, 2,            "sps_log2_ctu_size_minus5");
687
1.20k
  WRITE_FLAG(pcSPS->ptlDpbHrdParamsPresent,               "sps_ptl_dpb_hrd_params_present_flag");
688
689
1.20k
  if (pcSPS->ptlDpbHrdParamsPresent)
690
1.20k
  {
691
1.20k
    codeProfileTierLevel( &pcSPS->profileTierLevel, true, pcSPS->maxTLayers - 1 );
692
1.20k
  }
693
694
1.20k
  WRITE_FLAG(pcSPS->GDR,                                  "sps_gdr_enabled_flag");
695
1.20k
  WRITE_FLAG( pcSPS->rprEnabled,                          "sps_ref_pic_resampling_enabled_flag" );
696
1.20k
  if( pcSPS->rprEnabled )
697
0
  {
698
0
    WRITE_FLAG(pcSPS->resChangeInClvsEnabled, "sps_res_change_in_clvs_allowed_flag");
699
0
  }
700
1.20k
  WRITE_UVLC( pcSPS->maxPicWidthInLumaSamples,            "sps_pic_width_max_in_luma_samples" );
701
1.20k
  WRITE_UVLC( pcSPS->maxPicHeightInLumaSamples,           "sps_pic_height_max_in_luma_samples" );
702
703
1.20k
  const Window& conf = pcSPS->conformanceWindow;
704
1.20k
  WRITE_FLAG( conf.enabledFlag,                           "sps_conformance_window_flag" );
705
1.20k
  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.20k
  WRITE_FLAG(pcSPS->subPicInfoPresent,                    "sps_subpic_info_present_flag");
714
715
1.20k
  if (pcSPS->subPicInfoPresent)
716
0
  {
717
0
    THROW("no suppport");
718
0
  }
719
720
1.20k
  WRITE_UVLC( pcSPS->bitDepths[ CH_L ] - 8,               "sps_bitdepth_minus8" );
721
1.20k
  WRITE_FLAG( pcSPS->entropyCodingSyncEnabled,            "sps_entropy_coding_sync_enabled_flag" );
722
1.20k
  WRITE_FLAG( pcSPS->entryPointsPresent,                  "sps_entry_point_offsets_present_flag" );
723
1.20k
  WRITE_CODE( pcSPS->bitsForPOC-4, 4,                     "sps_log2_max_pic_order_cnt_lsb_minus4" );
724
1.20k
  WRITE_FLAG( pcSPS->pocMsbFlag,                          "sps_poc_msb_flag");
725
726
1.20k
  if (pcSPS->pocMsbFlag)
727
0
  {
728
0
    WRITE_UVLC(pcSPS->pocMsbLen - 1,                      "sps_poc_msb_len_minus1");
729
0
  }
730
731
1.20k
  WRITE_CODE(0, 2,                                        "sps_num_extra_ph_bits_bytes");
732
1.20k
  WRITE_CODE(0, 2,                                        "sps_num_extra_sh_bits_bytes");
733
734
1.20k
  if (pcSPS->ptlDpbHrdParamsPresent)
735
1.20k
  {
736
1.20k
    if (pcSPS->maxTLayers > 1)
737
1.20k
    {
738
1.20k
      WRITE_FLAG(pcSPS->subLayerDpbParams,                "sps_sublayer_dpb_params_flag");
739
1.20k
    }
740
1.20k
    dpb_parameters(pcSPS->maxTLayers - 1, pcSPS->subLayerDpbParams, pcSPS);
741
1.20k
  }
742
743
1.20k
  WRITE_UVLC(pcSPS->log2MinCodingBlockSize - 2,                           "log2_min_luma_coding_block_size_minus2");
744
1.20k
  WRITE_FLAG(pcSPS->partitionOverrideEnabled,                             "sps_partition_constraints_override_enabled_flag");
745
1.20k
  WRITE_UVLC(Log2(pcSPS->minQTSize[0]) - pcSPS->log2MinCodingBlockSize,   "sps_log2_diff_min_qt_min_cb_intra_slice_luma");
746
1.20k
  WRITE_UVLC(pcSPS->maxMTTDepth[0],                                       "sps_max_mtt_hierarchy_depth_intra_slice_luma");
747
1.20k
  if (pcSPS->maxMTTDepth[0] != 0)
748
1.20k
  {
749
1.20k
    WRITE_UVLC(Log2(pcSPS->maxBTSize[0]) - Log2(pcSPS->minQTSize[0]),     "sps_log2_diff_max_bt_min_qt_intra_slice_luma");
750
1.20k
    WRITE_UVLC(Log2(pcSPS->maxTTSize[0]) - Log2(pcSPS->minQTSize[0]),     "sps_log2_diff_max_tt_min_qt_intra_slice_luma");
751
1.20k
  }
752
1.20k
  if( pcSPS->chromaFormatIdc != CHROMA_400 )
753
1.20k
  {
754
1.20k
    WRITE_FLAG(pcSPS->dualITree,                                          "sps_qtbtt_dual_tree_intra_flag");
755
1.20k
  }
756
1.20k
  if (pcSPS->dualITree)
757
1.20k
  {
758
1.20k
    WRITE_UVLC(Log2(pcSPS->minQTSize[2]) - pcSPS->log2MinCodingBlockSize, "sps_log2_diff_min_qt_min_cb_intra_slice_chroma");
759
1.20k
    WRITE_UVLC(pcSPS->maxMTTDepth[2],                                     "sps_max_mtt_hierarchy_depth_intra_slice_chroma");
760
1.20k
    if (pcSPS->maxMTTDepth[2] != 0)
761
1.20k
    {
762
1.20k
      WRITE_UVLC(Log2(pcSPS->maxBTSize[2]) - Log2(pcSPS->minQTSize[2]),   "sps_log2_diff_max_bt_min_qt_intra_slice_chroma");
763
1.20k
      WRITE_UVLC(Log2(pcSPS->maxTTSize[2]) - Log2(pcSPS->minQTSize[2]),   "sps_log2_diff_max_tt_min_qt_intra_slice_chroma");
764
1.20k
    }
765
1.20k
  }
766
767
1.20k
  WRITE_UVLC(Log2(pcSPS->minQTSize[1]) - pcSPS->log2MinCodingBlockSize,   "sps_log2_diff_min_qt_min_cb_inter_slice");
768
1.20k
  WRITE_UVLC(pcSPS->maxMTTDepth[1],                                       "sps_max_mtt_hierarchy_depth_inter_slice");
769
1.20k
  if (pcSPS->maxMTTDepth[1] != 0)
770
1.20k
  {
771
1.20k
    WRITE_UVLC(Log2(pcSPS->maxBTSize[1]) - Log2(pcSPS->minQTSize[1]),     "sps_log2_diff_max_bt_min_qt_inter_slice");
772
1.20k
    WRITE_UVLC(Log2(pcSPS->maxTTSize[1]) - Log2(pcSPS->minQTSize[1]),     "sps_log2_diff_max_tt_min_qt_inter_slice");
773
1.20k
  }
774
775
1.20k
  if (pcSPS->CTUSize > 32)
776
1.20k
  {
777
1.20k
    WRITE_FLAG( (pcSPS->log2MaxTbSize - 5) != 0,                          "sps_max_luma_transform_size_64_flag" );
778
1.20k
  }
779
1.20k
  WRITE_FLAG(pcSPS->transformSkip,                                        "sps_transform_skip_enabled_flag");
780
1.20k
  if (pcSPS->transformSkip)
781
1.20k
  {
782
1.20k
    WRITE_UVLC(pcSPS->log2MaxTransformSkipBlockSize - 2,                  "sps_log2_transform_skip_max_size_minus2");
783
1.20k
    WRITE_FLAG(pcSPS->BDPCM,                                              "sps_bdpcm_enabled_flag");
784
1.20k
  }
785
1.20k
  WRITE_FLAG( pcSPS->MTS,                                                 "sps_mts_enabled_flag" );
786
1.20k
  if ( pcSPS->MTS )
787
1.20k
  {
788
1.20k
    WRITE_FLAG( pcSPS->MTSIntra,                                          "sps_explicit_mts_intra_enabled_flag" );
789
1.20k
    WRITE_FLAG( pcSPS->MTSInter,                                          "sps_explicit_mts_inter_enabled_flag" );
790
1.20k
  }
791
1.20k
  WRITE_FLAG( pcSPS->LFNST,                                               "sps_lfnst_enabled_flag");
792
793
1.20k
  if (pcSPS->chromaFormatIdc != CHROMA_400)
794
1.20k
  {
795
1.20k
    WRITE_FLAG(pcSPS->jointCbCr,                                          "sps_joint_cbcr_enabled_flag");
796
797
1.20k
    const ChromaQpMappingTable& chromaQpMappingTable = pcSPS->chromaQpMappingTable;
798
1.20k
    WRITE_FLAG(chromaQpMappingTable.m_sameCQPTableForAllChromaFlag,       "same_qp_table_for_chroma");
799
1.20k
    int numQpTables = chromaQpMappingTable.m_sameCQPTableForAllChromaFlag ? 1 : (pcSPS->jointCbCr ? 3 : 2);
800
1.20k
    CHECK(numQpTables != chromaQpMappingTable.m_numQpTables, " numQpTables does not match at encoder side ");
801
2.40k
    for (int i = 0; i < numQpTables; i++)
802
1.20k
    {
803
1.20k
      WRITE_SVLC(chromaQpMappingTable.m_qpTableStartMinus26[i],           "sps_qp_table_starts_minus26");
804
1.20k
      WRITE_UVLC(chromaQpMappingTable.m_numPtsInCQPTableMinus1[i],        "sps_num_points_in_qp_table_minus1");
805
806
4.80k
      for (int j = 0; j <= chromaQpMappingTable.m_numPtsInCQPTableMinus1[i]; j++)
807
3.60k
      {
808
3.60k
        WRITE_UVLC(chromaQpMappingTable.m_deltaQpInValMinus1[i][j],       "sps_delta_qp_in_val_minus1");
809
3.60k
        WRITE_UVLC(chromaQpMappingTable.m_deltaQpOutVal[i][j] ^ chromaQpMappingTable.m_deltaQpInValMinus1[i][j], "sps_delta_qp_diff_val");
810
3.60k
      }
811
1.20k
    }
812
1.20k
  }
813
814
1.20k
  WRITE_FLAG( pcSPS->saoEnabled,                          "sps_sao_enabled_flag");
815
1.20k
  WRITE_FLAG( pcSPS->alfEnabled,                          "sps_alf_enabled_flag" );
816
1.20k
  if (pcSPS->alfEnabled && pcSPS->chromaFormatIdc != CHROMA_400)
817
1.20k
  {
818
1.20k
    WRITE_FLAG( pcSPS->ccalfEnabled,                      "sps_ccalf_enabled_flag" );
819
1.20k
  }
820
1.20k
  WRITE_FLAG( false,                                      "sps_lmcs_enable_flag");
821
1.20k
  WRITE_FLAG( pcSPS->weightPred,                          "sps_weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
822
1.20k
  WRITE_FLAG( pcSPS->weightedBiPred,                      "sps_weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
823
1.20k
  WRITE_FLAG( pcSPS->longTermRefsPresent,                 "sps_long_term_ref_pics_flag" );
824
1.20k
  if( pcSPS->vpsId > 0 )
825
0
  {
826
0
    WRITE_FLAG( pcSPS->interLayerPresent,                 "sps_inter_layer_ref_pics_present_flag" );
827
0
  }
828
1.20k
  WRITE_FLAG( pcSPS->idrRefParamList,                     "sps_idr_rpl_present_flag" );
829
1.20k
  WRITE_FLAG( pcSPS->rpl1CopyFromRpl0,                    "sps_rpl1_copy_from_rpl0_flag");
830
831
  //Write candidate for List0
832
1.20k
  uint32_t numberOfRPL = (uint32_t)pcSPS->getNumRPL(0);
833
1.20k
  WRITE_UVLC(numberOfRPL,                                 "sps_num_ref_pic_lists_in_sps[0]");
834
30.0k
  for (int ii = 0; ii < numberOfRPL; ii++)
835
28.8k
  {
836
28.8k
    xCodeRefPicList( &pcSPS->rplList[0][ii], pcSPS->longTermRefsPresent, pcSPS->bitsForPOC, !pcSPS->weightPred && !pcSPS->weightedBiPred, ii );
837
28.8k
  }
838
839
  //Write candidate for List1
840
1.20k
  if (!pcSPS->rpl1CopyFromRpl0)
841
1.20k
  {
842
1.20k
    numberOfRPL = (uint32_t)pcSPS->getNumRPL(1);
843
1.20k
    WRITE_UVLC(numberOfRPL,                               "sps_num_ref_pic_lists_in_sps[1]");
844
30.0k
    for (int ii = 0; ii < numberOfRPL; ii++)
845
28.8k
    {
846
28.8k
      xCodeRefPicList( &pcSPS->rplList[1][ii], pcSPS->longTermRefsPresent, pcSPS->bitsForPOC, !pcSPS->weightPred && !pcSPS->weightedBiPred, ii );
847
28.8k
    }
848
1.20k
  }
849
850
1.20k
  WRITE_FLAG( pcSPS->wrapAroundEnabled,                   "sps_ref_wraparound_enabled_flag" );
851
852
1.20k
  WRITE_FLAG( pcSPS->temporalMVPEnabled,                  "sps_temporal_mvp_enabled_flag" );
853
854
1.20k
  if ( pcSPS->temporalMVPEnabled )
855
1.20k
  {
856
1.20k
    WRITE_FLAG( pcSPS->SbtMvp,                            "sps_sbtmvp_enabled_flag");
857
1.20k
  }
858
859
1.20k
  WRITE_FLAG( pcSPS->AMVR,                                "sps_amvr_enabled_flag" );
860
861
1.20k
  WRITE_FLAG( pcSPS->BDOF,                                "sps_bdof_enabled_flag" );
862
1.20k
  if (pcSPS->BDOF)
863
1.20k
  {
864
1.20k
    WRITE_FLAG(pcSPS->BdofPresent,                        "sps_bdof_pic_present_flag");
865
1.20k
  }
866
1.20k
  WRITE_FLAG( pcSPS->SMVD,                                "sps_smvd_enabled_flag" );
867
1.20k
  WRITE_FLAG( pcSPS->DMVR,                                "sps_dmvr_enabled_flag" );
868
1.20k
  if (pcSPS->DMVR)
869
1.20k
  {
870
1.20k
    WRITE_FLAG(pcSPS->DmvrPresent,                        "sps_dmvr_pic_present_flag");
871
1.20k
  }
872
1.20k
  WRITE_FLAG(pcSPS->MMVD,                                 "sps_mmvd_enabled_flag");
873
1.20k
  if ( pcSPS->MMVD )
874
1.20k
  {
875
1.20k
    WRITE_FLAG( pcSPS->fpelMmvd,                          "sps_fpel_mmvd_enabled_flag" );
876
1.20k
  }
877
1.20k
  WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSPS->maxNumMergeCand,  "sps_six_minus_max_num_merge_cand");
878
1.20k
  WRITE_FLAG( pcSPS->SBT,                                 "sps_sbt_enabled_flag" );
879
1.20k
  WRITE_FLAG( pcSPS->Affine,                              "sps_affine_enabled_flag" );
880
1.20k
  if ( pcSPS->Affine )
881
1.20k
  {
882
1.20k
    WRITE_UVLC(AFFINE_MRG_MAX_NUM_CANDS - pcSPS->maxNumAffineMergeCand, "five_minus_max_num_subblock_merge_cand");
883
1.20k
    WRITE_FLAG( pcSPS->AffineType,                        "sps_affine_type_flag" );
884
1.20k
    if (pcSPS->AMVR )
885
1.20k
    {
886
1.20k
      WRITE_FLAG( pcSPS->AffineAmvr,                      "sps_affine_amvr_enabled_flag" );
887
1.20k
    }
888
889
1.20k
    WRITE_FLAG( pcSPS->PROF,                              "sps_affine_prof_enabled_flag" );
890
1.20k
    if (pcSPS->PROF)
891
1.20k
    {
892
1.20k
      WRITE_FLAG(pcSPS->ProfPresent,                      "sps_prof_pic_present_flag" );
893
1.20k
    }
894
1.20k
  }
895
896
1.20k
  WRITE_FLAG(pcSPS->BCW,                                  "sps_bcw_enabled_flag");
897
898
1.20k
  WRITE_FLAG( pcSPS->CIIP,                                "sps_ciip_enabled_flag" );
899
900
1.20k
  if (pcSPS->maxNumMergeCand >= 2)
901
1.20k
  {
902
1.20k
    WRITE_FLAG(pcSPS->GEO,                                "sps_gpm_enabled_flag");
903
1.20k
    if (pcSPS->GEO && pcSPS->maxNumMergeCand >= 3)
904
1.20k
    {
905
1.20k
      WRITE_UVLC(pcSPS->maxNumMergeCand - pcSPS->maxNumGeoCand,   "sps_max_num_merge_cand_minus_max_num_gpm_cand");
906
1.20k
    }
907
1.20k
  }
908
909
1.20k
  WRITE_UVLC(pcSPS->log2ParallelMergeLevelMinus2,         "sps_log2_parallel_merge_level_minus2");
910
1.20k
  WRITE_FLAG( pcSPS->ISP,                                 "sps_isp_enabled_flag");
911
1.20k
  WRITE_FLAG( pcSPS->MRL,                                 "sps_mrl_enabled_flag");
912
1.20k
  WRITE_FLAG( pcSPS->MIP,                                 "sps_mip_enabled_flag");
913
1.20k
  if( pcSPS->chromaFormatIdc != CHROMA_400)
914
1.20k
  {
915
1.20k
    WRITE_FLAG( pcSPS->LMChroma,                          "sps_cclm_enabled_flag" );
916
1.20k
  }
917
1.20k
  if ( pcSPS->chromaFormatIdc == CHROMA_420 )
918
1.20k
  {
919
1.20k
    WRITE_FLAG( pcSPS->horCollocatedChroma,               "sps_chroma_horizontal_collocated_flag" );
920
1.20k
    WRITE_FLAG( pcSPS->verCollocatedChroma,               "sps_chroma_vertical_collocated_flag" );
921
1.20k
  }
922
923
1.20k
  WRITE_FLAG(pcSPS->PLT,                                  "sps_palette_enabled_flag" );
924
925
1.20k
  if (pcSPS->chromaFormatIdc == CHROMA_444)
926
0
  {
927
0
    WRITE_FLAG(pcSPS->PLT,                                "sps_plt_enabled_flag" );
928
0
  }
929
1.20k
  if (pcSPS->chromaFormatIdc == CHROMA_444 && pcSPS->log2MaxTbSize != 6)
930
0
  {
931
0
    WRITE_FLAG(pcSPS->useColorTrans,                      "sps_act_enabled_flag");
932
0
  }
933
1.20k
  if (pcSPS->transformSkip || pcSPS->PLT)
934
1.20k
  {
935
1.20k
    WRITE_UVLC(pcSPS->internalMinusInputBitDepth[CH_L],   "sps_internal_bit_depth_minus_input_bit_depth");
936
1.20k
  }
937
938
1.20k
  WRITE_FLAG(pcSPS->IBC,                                  "sps_ibc_enabled_flag");
939
1.20k
  if( pcSPS->IBC )
940
1.20k
  {
941
1.20k
    WRITE_UVLC(IBC_MRG_MAX_NUM_CANDS - pcSPS->maxNumIBCMergeCand, "six_minus_max_num_ibc_merge_cand");
942
1.20k
  }
943
944
1.20k
  WRITE_FLAG( pcSPS->LADF,                                "sps_ladf_enabled_flag" );
945
1.20k
  if ( pcSPS->LADF )
946
0
  {
947
0
    THROW("no support");
948
0
  }
949
950
1.20k
  WRITE_FLAG( pcSPS->scalingListEnabled,                  "sps_explicit_scaling_list_enabled_flag" );
951
1.20k
  if (pcSPS->LFNST && pcSPS->scalingListEnabled )
952
0
  {
953
0
    WRITE_FLAG(pcSPS->disableScalingMatrixForLfnstBlks,   "sps_scaling_matrix_for_lfnst_disabled_flag");
954
0
  }
955
1.20k
  if (pcSPS->useColorTrans && pcSPS->scalingListEnabled)
956
0
  {
957
0
    WRITE_FLAG(pcSPS->scalingMatrixAlternativeColourSpaceDisabled, "sps_scaling_matrix_for_alternative_colour_space_disabled_flag");
958
0
  }
959
1.20k
  if (pcSPS->scalingMatrixAlternativeColourSpaceDisabled)
960
0
  {
961
0
    WRITE_FLAG(pcSPS->scalingMatrixDesignatedColourSpace, "sps_scaling_matrix_designated_colour_space_flag");
962
0
  }
963
1.20k
  WRITE_FLAG(pcSPS->depQuantEnabled,                      "sps_dep_quant_enabled_flag");
964
1.20k
  WRITE_FLAG(pcSPS->signDataHidingEnabled,                "sps_sign_data_hiding_enabled_flag");
965
966
1.20k
  WRITE_FLAG( pcSPS->virtualBoundariesEnabled,            "sps_virtual_boundaries_enabled_flag" );
967
1.20k
  if( pcSPS->virtualBoundariesEnabled )
968
0
  {
969
0
    WRITE_CODE( pcSPS->numVerVirtualBoundaries, 2,        "sps_num_ver_virtual_boundaries");
970
0
    for( unsigned i = 0; i < pcSPS->numVerVirtualBoundaries; i++ )
971
0
    {
972
0
      WRITE_UVLC((pcSPS->virtualBoundariesPosX[i]>>3),    "sps_virtual_boundaries_pos_x");
973
0
    }
974
0
    WRITE_CODE(pcSPS->numHorVirtualBoundaries, 2,         "sps_num_hor_virtual_boundaries");
975
0
    for( unsigned i = 0; i < pcSPS->numHorVirtualBoundaries; i++ )
976
0
    {
977
0
      WRITE_UVLC((pcSPS->virtualBoundariesPosY[i]>>3),    "sps_virtual_boundaries_pos_y");
978
0
    }
979
0
  }
980
981
1.20k
  if (pcSPS->ptlDpbHrdParamsPresent)
982
1.20k
  {
983
1.20k
    WRITE_FLAG(pcSPS->hrdParametersPresent,               "sps_timing_hrd_params_present_flag");
984
985
1.20k
    if( pcSPS->hrdParametersPresent )
986
1.20k
    {
987
1.20k
      codeGeneralHrdparameters(&pcSPS->generalHrdParams);
988
1.20k
      if ((pcSPS->maxTLayers - 1) > 0)
989
1.20k
      {
990
1.20k
        WRITE_FLAG(pcSPS->subLayerParametersPresent,      "sps_sublayer_cpb_params_present_flag");
991
1.20k
      }
992
1.20k
      uint32_t firstSubLayer = pcSPS->subLayerParametersPresent ? 0 : (pcSPS->maxTLayers - 1);
993
1.20k
      codeOlsHrdParameters(&pcSPS->generalHrdParams, pcSPS->olsHrdParams, firstSubLayer, pcSPS->maxTLayers - 1);
994
1.20k
    }
995
1.20k
  }
996
997
1.20k
  WRITE_FLAG(pcSPS->fieldSeqFlag,                         "sps_field_seq_flag");
998
999
1.20k
  WRITE_FLAG( pcSPS->vuiParametersPresent,                "sps_vui_parameters_present_flag" );
1000
1.20k
  if (pcSPS->vuiParametersPresent)
1001
0
  {
1002
0
    OutputBitstream *bs = m_pcBitIf; // save the original ono
1003
0
    OutputBitstream bs_count;
1004
0
    setBitstream(&bs_count);
1005
#if ENABLE_TRACING
1006
    bool traceEnable = g_HLSTraceEnable;
1007
    g_HLSTraceEnable = false;
1008
#endif
1009
0
    codeVUI(&pcSPS->vuiParameters, pcSPS);
1010
#if ENABLE_TRACING
1011
    g_HLSTraceEnable = traceEnable;
1012
#endif
1013
0
    unsigned vui_payload_data_num_bits = bs_count.getNumberOfWrittenBits();
1014
0
    CHECK( vui_payload_data_num_bits % 8 != 0, "Invalid number of VUI payload data bits" );
1015
0
    setBitstream(bs);
1016
0
    WRITE_UVLC((vui_payload_data_num_bits >> 3) - 1,        "sps_vui_payload_size_minus1");
1017
0
    while (!isByteAligned())
1018
0
    {
1019
0
      WRITE_FLAG(0,                                         "sps_vui_alignment_zero_bit");
1020
0
    }
1021
0
    codeVUI(&pcSPS->vuiParameters, pcSPS);
1022
0
  }
1023
1024
1.20k
  bool sps_extension_present_flag=false;
1025
1.20k
  bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS]={false};
1026
1027
10.8k
  for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
1028
9.60k
  {
1029
9.60k
    sps_extension_present_flag|=sps_extension_flags[i];
1030
9.60k
  }
1031
1032
1.20k
  WRITE_FLAG( sps_extension_present_flag,                   "sps_extension_present_flag" );
1033
1034
1.20k
  if (sps_extension_present_flag)
1035
0
  {
1036
#if ENABLE_TRACING
1037
    static const char *syntaxStrings[]={ "sps_range_extension_flag",
1038
      "sps_multilayer_extension_flag",
1039
      "sps_extension_6bits[0]",
1040
      "sps_extension_6bits[1]",
1041
      "sps_extension_6bits[2]",
1042
      "sps_extension_6bits[3]",
1043
      "sps_extension_6bits[4]",
1044
      "sps_extension_6bits[5]" };
1045
#endif
1046
1047
0
    for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
1048
0
    {
1049
0
      WRITE_FLAG( sps_extension_flags[i], syntaxStrings[i] );
1050
0
    }
1051
1052
0
    for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
1053
0
    {
1054
0
      if (sps_extension_flags[i])
1055
0
      {
1056
#if 0 // TODO: enable when applicable
1057
        switch (SPSExtensionFlagIndex(i))
1058
        {
1059
        default:
1060
          CHECK(sps_extension_flags[i]!=false, "Unknown PPS extension signalled"); // Should never get here with an active SPS extension flag.
1061
          break;
1062
        }
1063
#endif
1064
0
      }
1065
0
    }
1066
0
  }
1067
1.20k
  xWriteRbspTrailingBits();
1068
1.20k
}
1069
1070
void HLSWriter::codeDCI( const DCI* dci )
1071
0
{
1072
0
  DTRACE( g_trace_ctx, D_HEADER, "=========== Decoding Parameter Set     ===========\n" );
1073
1074
0
  WRITE_CODE( 0,                                    4,        "dci_reserved_zero_5bits" );
1075
0
  uint32_t numPTLs = (uint32_t) dci->profileTierLevel.size();
1076
0
  CHECK (numPTLs<1, "At least one PTL must be available in DPS");
1077
1078
0
  WRITE_CODE( numPTLs - 1,                          4,        "dci_num_ptls_minus1" );
1079
1080
0
  for (int i=0; i< numPTLs; i++)
1081
0
  {
1082
0
    codeProfileTierLevel( &dci->profileTierLevel[i], true, 0 );
1083
0
  }
1084
0
  WRITE_FLAG( 0,                                              "dci_extension_flag" );
1085
0
  xWriteRbspTrailingBits();
1086
0
}
1087
1088
void HLSWriter::codeVPS(const VPS* pcVPS)
1089
0
{
1090
0
  DTRACE( g_trace_ctx, D_HEADER, "=========== Video Parameter Set     ===========\n" );
1091
1092
0
  WRITE_CODE(pcVPS->vpsId,              4,              "vps_video_parameter_set_id");
1093
0
  WRITE_CODE(pcVPS->maxLayers - 1,      6,              "vps_max_layers_minus1");
1094
0
  WRITE_CODE(pcVPS->maxSubLayers - 1,   3,              "vps_max_sublayers_minus1");
1095
0
  if (pcVPS->maxLayers > 1 && pcVPS->maxSubLayers > 1)
1096
0
  {
1097
0
    WRITE_FLAG(pcVPS->defaultPtlDpbHrdMaxTidFlag,        "vps_default_ptl_dpb_hrd_max_tid_flag");
1098
0
  }
1099
0
  if (pcVPS->maxLayers > 1)
1100
0
  {
1101
0
    WRITE_FLAG(pcVPS->allIndependentLayers,             "vps_all_independent_layers_flag");
1102
0
  }
1103
0
  for (uint32_t i = 0; i < pcVPS->maxLayers; i++)
1104
0
  {
1105
0
    WRITE_CODE(pcVPS->layerId[i], 6,                    "vps_layer_id");
1106
0
    if (i > 0 && !pcVPS->allIndependentLayers)
1107
0
    {
1108
0
      WRITE_FLAG(pcVPS->independentLayer[i],            "vps_independent_layer_flag");
1109
0
      if (!pcVPS->independentLayer[i])
1110
0
      {
1111
0
        bool presentFlag = false;
1112
0
        for (int j = 0; j < i; j++)
1113
0
        {
1114
0
          presentFlag |= ((pcVPS->maxTidIlRefPicsPlus1[i][j] != VVENC_MAX_TLAYER) && pcVPS->directRefLayer[i][j]);
1115
0
        }
1116
0
        WRITE_FLAG(presentFlag, "max_tid_ref_present_flag[ i ]");
1117
0
        for (int j = 0; j < i; j++)
1118
0
        {
1119
0
          WRITE_FLAG(pcVPS->directRefLayer[i][j], "vps_direct_ref_layer_flag");
1120
0
          if (presentFlag && pcVPS->directRefLayer[i][j])
1121
0
          {
1122
0
            WRITE_CODE(pcVPS->maxTidIlRefPicsPlus1[i][j], 3, "max_tid_il_ref_pics_plus1[ i ][ j ]");
1123
0
          }
1124
0
        }
1125
0
      }
1126
0
    }
1127
0
  }
1128
0
  if( pcVPS->maxLayers > 1 )
1129
0
  {
1130
0
    if (pcVPS->allIndependentLayers)
1131
0
    {
1132
0
      WRITE_FLAG(pcVPS->eachLayerIsAnOls,               "vps_each_layer_is_an_ols_flag");
1133
0
    }
1134
0
    if (!pcVPS->eachLayerIsAnOls)
1135
0
    {
1136
0
      if (!pcVPS->allIndependentLayers)
1137
0
      {
1138
0
        WRITE_CODE(pcVPS->olsModeIdc, 2,                "vps_ols_mode_idc");
1139
0
      }
1140
0
      if (pcVPS->olsModeIdc == 2)
1141
0
      {
1142
0
        WRITE_CODE(pcVPS->numOutputLayerSets - 2, 8,    "vps_num_output_layer_sets_minus2");
1143
0
        for (uint32_t i = 1; i < pcVPS->numOutputLayerSets; i++)
1144
0
        {
1145
0
          for (uint32_t j = 0; j < pcVPS->maxLayers; j++)
1146
0
          {
1147
0
            WRITE_FLAG(pcVPS->olsOutputLayer[i][j],     "vps_ols_output_layer_flag");
1148
0
          }
1149
0
        }
1150
0
      }
1151
0
    }
1152
0
    CHECK(pcVPS->numPtls - 1 >= pcVPS->totalNumOLSs, "vps_num_ptls_minus1 shall be less than TotalNumOlss");
1153
0
    WRITE_CODE(pcVPS->numPtls - 1, 8,                   "vps_num_ptls_minus1");
1154
0
  }
1155
1156
0
  int totalNumOlss = pcVPS->totalNumOLSs;
1157
0
  for (int i = 0; i < pcVPS->numPtls; i++)
1158
0
  {
1159
0
    if(i > 0)
1160
0
    {
1161
0
      WRITE_FLAG(pcVPS->ptPresent[i],                   "vps_ptl_present_flag");
1162
0
    }
1163
0
    if(!pcVPS->allLayersSameNumSubLayers)
1164
0
    {
1165
0
      WRITE_CODE(pcVPS->ptlMaxTemporalId[i] ,3,         "vps_ptl_max_temporal_id");
1166
0
    }
1167
0
  }
1168
0
  int cnt = 0;
1169
0
  while (m_pcBitIf->getNumBitsUntilByteAligned())
1170
0
  {
1171
0
    WRITE_FLAG( 0,                                      "vps_ptl_reserved_zero_bit");
1172
0
    cnt++;
1173
0
  }
1174
0
  CHECK(cnt>=8, "More than '8' alignment bytes written");
1175
0
  for (int i = 0; i < pcVPS->numPtls; i++)
1176
0
  {
1177
0
    codeProfileTierLevel(&pcVPS->profileTierLevel[i], pcVPS->ptPresent[i], pcVPS->ptlMaxTemporalId[i] - 1);
1178
0
  }
1179
0
  for (int i = 0; i < totalNumOlss; i++)
1180
0
  {
1181
0
    if(pcVPS->numPtls > 1 && pcVPS->numPtls != pcVPS->totalNumOLSs)
1182
0
      WRITE_CODE(pcVPS->olsPtlIdx[i], 8,                "vps_ols_ptl_idx");
1183
0
  }
1184
0
  if( !pcVPS->allIndependentLayers )
1185
0
  {
1186
0
    WRITE_UVLC( pcVPS->numDpbParams,                    "vps_num_dpb_params" );
1187
0
  }
1188
1189
0
  if( pcVPS->numDpbParams > 0 && pcVPS->maxSubLayers > 1 )
1190
0
  {
1191
0
    WRITE_FLAG( pcVPS->sublayerDpbParamsPresent,        "vps_sublayer_dpb_params_present_flag" );
1192
0
  }
1193
1194
0
  for( int i = 0; i < pcVPS->numDpbParams; i++ )
1195
0
  {
1196
0
    if( !pcVPS->allLayersSameNumSubLayers )
1197
0
    {
1198
0
      WRITE_CODE( pcVPS->dpbMaxTemporalId[i], 3,      "vps_dpb_max_temporal_id[i]" );
1199
0
    }
1200
0
    if( pcVPS->maxSubLayers == 1 )
1201
0
    {
1202
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" );
1203
0
    }
1204
0
    else
1205
0
    {
1206
0
      if( pcVPS->defaultPtlDpbHrdMaxTidFlag )
1207
0
      {
1208
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" );
1209
0
      }
1210
0
      else
1211
0
      {
1212
0
        WRITE_CODE( pcVPS->dpbMaxTemporalId[i], 3,      "vps_dpb_max_temporal_id[i]" );
1213
0
      }
1214
0
    }
1215
1216
0
    for( int j = ( pcVPS->sublayerDpbParamsPresent ? 0 : pcVPS->dpbMaxTemporalId[i] ); j <= pcVPS->dpbMaxTemporalId[i]; j++ )
1217
0
    {
1218
0
      WRITE_UVLC( pcVPS->dpbParameters[i].maxDecPicBuffering[j],      "max_dec_pic_buffering_minus1[i]" );
1219
0
      WRITE_UVLC( pcVPS->dpbParameters[i].numReorderPics[j],          "max_num_reorder_pics[i]" );
1220
0
      WRITE_UVLC( pcVPS->dpbParameters[i].maxLatencyIncreasePlus1[j], "max_latency_increase_plus1[i]" );
1221
0
    }
1222
0
  }
1223
1224
0
  for( int i = 0; i < pcVPS->totalNumOLSs; i++ )
1225
0
  {
1226
0
    if( pcVPS->numLayersInOls[i] > 1 )
1227
0
    {
1228
0
      WRITE_UVLC( pcVPS->olsDpbPicSize[i].width,          "vps_ols_dpb_pic_width[i]" );
1229
0
      WRITE_UVLC( pcVPS->olsDpbPicSize[i].height,         "vps_ols_dpb_pic_height[i]" );
1230
0
      WRITE_CODE( pcVPS->olsDpbChromaFormatIdc[i], 2,     "vps_ols_dpb_chroma_format[i]");
1231
0
      WRITE_UVLC( pcVPS->olsDpbBitDepthMinus8[i],         "vps_ols_dpb_bitdepth_minus8[i]");
1232
0
      if( pcVPS->numDpbParams > 1 && (pcVPS->numDpbParams != pcVPS->numMultiLayeredOlss) )
1233
0
      {
1234
0
        WRITE_UVLC( pcVPS->olsDpbParamsIdx[i],            "vps_ols_dpb_params_idx[i]" );
1235
0
      }
1236
0
    }
1237
0
  }
1238
1239
1240
0
  if (!pcVPS->eachLayerIsAnOls)
1241
0
  {
1242
0
    WRITE_FLAG(pcVPS->generalHrdParamsPresent, "vps_general_hrd_params_present_flag");
1243
0
  }
1244
0
  if (pcVPS->generalHrdParamsPresent)
1245
0
  {
1246
0
    codeGeneralHrdparameters(&pcVPS->generalHrdParams);
1247
0
    if ((pcVPS->maxSubLayers-1) > 0)
1248
0
    {
1249
0
      WRITE_FLAG(pcVPS->sublayerCpbParamsPresent, "vps_sublayer_cpb_params_present_flag");
1250
0
    }
1251
0
    WRITE_UVLC(pcVPS->numOlsHrdParamsMinus1, "vps_num_ols_hrd_params_minus1");
1252
0
    for (int i = 0; i <= pcVPS->numOlsHrdParamsMinus1; i++)
1253
0
    {
1254
0
      if (!pcVPS->defaultPtlDpbHrdMaxTidFlag)
1255
0
      {
1256
0
        WRITE_CODE(pcVPS->hrdMaxTid[i], 3, "vps_hrd_vps_max_tid[i]");
1257
0
      }
1258
0
      uint32_t firstSublayer = pcVPS->sublayerCpbParamsPresent ? 0 : pcVPS->hrdMaxTid[i];
1259
0
      codeOlsHrdParameters(&pcVPS->generalHrdParams, &pcVPS->olsHrdParams[i], firstSublayer, pcVPS->hrdMaxTid[i]);
1260
0
    }
1261
0
    if ((pcVPS->numOlsHrdParamsMinus1 > 0) && ((pcVPS->numOlsHrdParamsMinus1 + 1) != pcVPS->numMultiLayeredOlss))
1262
0
    {
1263
0
      for (int i = 0; i < pcVPS->numMultiLayeredOlss; i++)
1264
0
      {
1265
0
        WRITE_UVLC(pcVPS->olsHrdIdx[i], "vps_ols_hrd_idx[i]");
1266
0
      }
1267
0
    }
1268
0
  }
1269
0
  WRITE_FLAG(0,                                           "vps_extension_flag");
1270
1271
  //future extensions here..
1272
0
  xWriteRbspTrailingBits();
1273
0
}
1274
1275
void HLSWriter::codePictureHeader( const PicHeader* picHeader, bool writeRbspTrailingBits )
1276
1.20k
{
1277
1.20k
  const PPS*  pps = NULL;
1278
1.20k
  const SPS*  sps = NULL;
1279
1280
1.20k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Picture Header ===========\n" );
1281
1282
1.20k
  CodingStructure& cs = *picHeader->pic->cs;
1283
1.20k
  WRITE_FLAG(picHeader->gdrOrIrapPic, "ph_gdr_or_irap_pic_flag");
1284
1.20k
  WRITE_FLAG(picHeader->nonRefPic,    "ph_non_ref_pic_flag");
1285
1.20k
  if (picHeader->gdrOrIrapPic)
1286
1.20k
  {
1287
1.20k
    WRITE_FLAG(picHeader->gdrPic,     "ph_gdr_pic_flag");
1288
1.20k
  }
1289
  // Q0781, two-flags
1290
1.20k
  WRITE_FLAG(picHeader->picInterSliceAllowed,   "ph_inter_slice_allowed_flag");
1291
1.20k
  if (picHeader->picInterSliceAllowed)
1292
0
  {
1293
0
    WRITE_FLAG(picHeader->picIntraSliceAllowed, "ph_intra_slice_allowed_flag");
1294
0
  }
1295
  // parameter sets
1296
1.20k
  WRITE_UVLC(picHeader->ppsId,                  "ph_pic_parameter_set_id");
1297
1.20k
  pps = cs.slice->pps;
1298
1.20k
  CHECK(pps == 0, "Invalid PPS");
1299
1.20k
  sps = cs.slice->sps;
1300
1.20k
  CHECK(sps == 0, "Invalid SPS");
1301
1.20k
  int pocBits = cs.slice->sps->bitsForPOC;
1302
1.20k
  int pocMask = (1 << pocBits) - 1;
1303
1.20k
  WRITE_CODE(cs.slice->poc & pocMask, pocBits,  "ph_pic_order_cnt_lsb");
1304
1.20k
  if( picHeader->gdrPic )
1305
0
  {
1306
0
    WRITE_UVLC(picHeader->recoveryPocCnt,       "ph_recovery_poc_cnt");
1307
0
  }
1308
1309
  // PH extra bits are not written in the reference encoder
1310
  // as these bits are reserved for future extensions
1311
  // for( i = 0; i < NumExtraPhBits; i++ )
1312
  //    ph_extra_bit[ i ]
1313
1314
1.20k
  if (sps->pocMsbFlag)
1315
0
  {
1316
0
    WRITE_FLAG(picHeader->pocMsbPresent,        "ph_poc_msb_present_flag");
1317
0
    if (picHeader->pocMsbPresent)
1318
0
    {
1319
0
      WRITE_CODE(picHeader->pocMsbVal, sps->pocMsbLen, "ph_poc_msb_val");
1320
0
    }
1321
0
  }
1322
1323
   // alf enable flags and aps IDs
1324
1.20k
  if( sps->alfEnabled)
1325
1.20k
  {
1326
1.20k
    if (pps->alfInfoInPh)
1327
0
    {
1328
0
      WRITE_FLAG(picHeader->alfEnabled[COMP_Y],         "ph_alf_enabled_flag");
1329
0
      if (picHeader->alfEnabled[COMP_Y])
1330
0
      {
1331
0
        WRITE_CODE(picHeader->numAlfAps, 3,             "ph_num_alf_aps_ids_luma");
1332
0
        for (int i = 0; i < picHeader->numAlfAps; i++)
1333
0
        {
1334
0
          WRITE_CODE(picHeader->alfApsId[i], 3,         "ph_alf_aps_id_luma");
1335
0
        }
1336
1337
0
        const int alfChromaIdc = picHeader->alfEnabled[COMP_Cb] + picHeader->alfEnabled[COMP_Cr] * 2 ;
1338
0
        if (sps->chromaFormatIdc != CHROMA_400)
1339
0
        {
1340
0
          WRITE_CODE(picHeader->alfEnabled[COMP_Cb], 1, "ph_alf_cb_enabled_flag");
1341
0
          WRITE_CODE(picHeader->alfEnabled[COMP_Cr], 1, "ph_alf_cr_enabled_flag");
1342
0
        }
1343
0
        if (alfChromaIdc)
1344
0
        {
1345
0
          WRITE_CODE(picHeader->alfChromaApsId, 3,      "ph_alf_aps_id_chroma");
1346
0
        }
1347
0
        if (sps->ccalfEnabled)
1348
0
        {
1349
0
          WRITE_FLAG(picHeader->ccalfEnabled[COMP_Cb],  "ph_cc_alf_cb_enabled_flag");
1350
0
          if (picHeader->ccalfEnabled[COMP_Cb])
1351
0
          {
1352
0
            WRITE_CODE(picHeader->ccalfCbApsId, 3,      "ph_cc_alf_cb_aps_id");
1353
0
          }
1354
0
          WRITE_FLAG(picHeader->ccalfEnabled[COMP_Cr],  "ph_cc_alf_cr_enabled_flag");
1355
0
          if (picHeader->ccalfEnabled[COMP_Cr])
1356
0
          {
1357
0
            WRITE_CODE(picHeader->ccalfCrApsId, 3,      "ph_cc_alf_cr_aps_id");
1358
0
          }
1359
0
        }
1360
0
      }
1361
0
    }
1362
1.20k
  }
1363
1364
  // quantization scaling lists
1365
1.20k
  if( sps->scalingListEnabled )
1366
0
  {
1367
0
    WRITE_FLAG( picHeader->explicitScalingListEnabled,  "ph_scaling_list_present_flag" );
1368
0
    if( picHeader->explicitScalingListEnabled )
1369
0
    {
1370
0
      WRITE_CODE( picHeader->scalingListApsId, 3,       "ph_scaling_list_aps_id" );
1371
0
    }
1372
0
  }
1373
1374
  // virtual boundaries
1375
1.20k
  if( sps->virtualBoundariesEnabled && !sps->virtualBoundariesPresent )
1376
0
  {
1377
0
    WRITE_FLAG( picHeader->virtualBoundariesEnabled,    "ph_loop_filter_across_virtual_boundaries_disabled_present_flag" );
1378
0
    if( picHeader->virtualBoundariesEnabled )
1379
0
    {
1380
0
      WRITE_CODE(picHeader->numVerVirtualBoundaries, 2, "ph_num_ver_virtual_boundaries");
1381
0
      for( unsigned i = 0; i < picHeader->numVerVirtualBoundaries; i++ )
1382
0
      {
1383
0
        WRITE_UVLC(picHeader->virtualBoundariesPosX[i] >> 3, "ph_virtual_boundaries_pos_x");
1384
0
      }
1385
0
      WRITE_CODE(picHeader->numHorVirtualBoundaries, 2, "ph_num_hor_virtual_boundaries");
1386
0
      for( unsigned i = 0; i < picHeader->numHorVirtualBoundaries; i++ )
1387
0
      {
1388
0
        WRITE_UVLC(picHeader->virtualBoundariesPosY[i]>>3, "ph_virtual_boundaries_pos_y");
1389
0
      }
1390
0
    }
1391
0
  }
1392
1393
  // picture output flag
1394
1.20k
  if( pps->outputFlagPresent && !picHeader->nonRefPic)
1395
0
  {
1396
0
    WRITE_FLAG( picHeader->picOutputFlag, "ph_pic_output_flag" );
1397
0
  }
1398
1399
  // reference picture lists
1400
1.20k
  if (pps->rplInfoInPh)
1401
0
  {
1402
    // List0 and List1
1403
0
    for(int listIdx = 0; listIdx < 2; listIdx++)
1404
0
    {
1405
0
      if(sps->getNumRPL(listIdx) > 0 &&
1406
0
          (listIdx == 0 || (listIdx == 1 && pps->rpl1IdxPresent)))
1407
0
      {
1408
0
        WRITE_FLAG(picHeader->rplIdx[listIdx] != -1 ? 1 : 0, "pic_rpl_sps_flag[i]");
1409
0
      }
1410
0
      else if(sps->getNumRPL(listIdx) == 0)
1411
0
      {
1412
0
        CHECK(picHeader->rplIdx[listIdx] != -1, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1413
0
      }
1414
0
      else if(listIdx == 1)
1415
0
      {
1416
0
        auto rplsSpsFlag0 = picHeader->rplIdx[0] != -1 ? 1 : 0;
1417
0
        auto rplsSpsFlag1 = picHeader->rplIdx[1] != -1 ? 1 : 0;
1418
0
        CHECK(rplsSpsFlag1 != rplsSpsFlag0, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1419
0
      }
1420
1421
0
      if(picHeader->rplIdx[listIdx] != -1)
1422
0
      {
1423
0
        if(sps->getNumRPL(listIdx) > 1 &&
1424
0
            (listIdx == 0 || (listIdx == 1 && pps->rpl1IdxPresent)))
1425
0
        {
1426
0
          int numBits = ceilLog2(sps->getNumRPL( listIdx ));
1427
0
          WRITE_CODE(picHeader->rplIdx[listIdx], numBits, "pic_rpl_idx[i]");
1428
0
        }
1429
0
        else if(sps->getNumRPL(listIdx) == 1)
1430
0
        {
1431
0
          CHECK(picHeader->rplIdx[listIdx] != 0, "RPL1Idx is not signalled but it is not equal to 0");
1432
0
        }
1433
0
        else
1434
0
        {
1435
0
          CHECK(picHeader->rplIdx[1] != picHeader->rplIdx[0], "RPL1Idx is not signalled but it is not the same as RPL0Idx");
1436
0
        }
1437
0
      }
1438
      // explicit RPL in picture header
1439
0
      else
1440
0
      {
1441
0
        xCodeRefPicList( picHeader->pRPL[listIdx], sps->longTermRefsPresent, sps->bitsForPOC, !(sps->weightPred||sps->weightedBiPred), -1 );
1442
0
      }
1443
1444
      // POC MSB cycle signalling for LTRP
1445
0
      if (picHeader->pRPL[listIdx]->numberOfLongtermPictures)
1446
0
      {
1447
0
        for (int i = 0; i < picHeader->pRPL[listIdx]->numberOfLongtermPictures + picHeader->pRPL[listIdx]->numberOfShorttermPictures; i++)
1448
0
        {
1449
0
          if (picHeader->pRPL[listIdx]->isLongtermRefPic[i])
1450
0
          {
1451
0
            if (picHeader->pRPL[listIdx]->ltrpInSliceHeader)
1452
0
            {
1453
0
              WRITE_CODE(picHeader->pRPL[listIdx]->refPicIdentifier[i], sps->bitsForPOC,
1454
0
                         "pic_poc_lsb_lt[listIdx][rplsIdx][j]");
1455
0
            }
1456
0
            WRITE_FLAG(picHeader->pRPL[listIdx]->deltaPocMSBPresent[i], "pic_delta_poc_msb_present_flag[i][j]");
1457
0
            if (picHeader->pRPL[listIdx]->deltaPocMSBPresent[i])
1458
0
            {
1459
0
              WRITE_UVLC(picHeader->pRPL[listIdx]->deltaPocMSBCycleLT[i], "pic_delta_poc_msb_cycle_lt[i][j]");
1460
0
            }
1461
0
          }
1462
0
        }
1463
0
      }
1464
0
    }
1465
0
  }
1466
1467
  // partitioning constraint overrides
1468
1.20k
  if (sps->partitionOverrideEnabled )
1469
1.20k
  {
1470
1.20k
    WRITE_FLAG(picHeader->splitConsOverride, "partition_constraints_override_flag");
1471
1.20k
  }
1472
1473
  // Q0781, two-flags
1474
1.20k
  if (picHeader->picIntraSliceAllowed)
1475
1.20k
  {
1476
1.20k
    if (picHeader->splitConsOverride)
1477
0
    {
1478
0
      WRITE_UVLC(floorLog2(picHeader->minQTSize[0]) - sps->log2MinCodingBlockSize, "pic_log2_diff_min_qt_min_cb_intra_slice_luma");
1479
0
      WRITE_UVLC(picHeader->maxMTTDepth[0], "ph_max_mtt_hierarchy_depth_intra_slice_luma");
1480
0
      if (picHeader->maxMTTDepth[0] != 0)
1481
0
      {
1482
0
        WRITE_UVLC(floorLog2(picHeader->maxBTSize[0]) - floorLog2(picHeader->minQTSize[0]), "ph_log2_diff_max_bt_min_qt_intra_slice_luma");
1483
0
        WRITE_UVLC(floorLog2(picHeader->maxTTSize[0]) - floorLog2(picHeader->minQTSize[0]), "ph_log2_diff_max_tt_min_qt_intra_slice_luma");
1484
0
      }
1485
1486
0
      if (sps->dualITree)
1487
0
      {
1488
0
        WRITE_UVLC(floorLog2(picHeader->minQTSize[2]) - sps->log2MinCodingBlockSize, "ph_log2_diff_min_qt_min_cb_intra_slice_chroma");
1489
0
        WRITE_UVLC(picHeader->maxMTTDepth[2], "ph_max_mtt_hierarchy_depth_intra_slice_chroma");
1490
0
        if (picHeader->maxMTTDepth[2] != 0)
1491
0
        {
1492
0
          WRITE_UVLC(floorLog2(picHeader->maxBTSize[2]) - floorLog2(picHeader->minQTSize[2]), "ph_log2_diff_max_bt_min_qt_intra_slice_chroma");
1493
0
          WRITE_UVLC(floorLog2(picHeader->maxTTSize[2]) - floorLog2(picHeader->minQTSize[2]), "ph_log2_diff_max_tt_min_qt_intra_slice_chroma");
1494
0
        }
1495
0
      }
1496
0
    }
1497
1.20k
  }
1498
1.20k
  if (picHeader->picIntraSliceAllowed )
1499
1.20k
  {
1500
  // delta quantization and chrom and chroma offset
1501
1.20k
    if (pps->useDQP)
1502
1.20k
    {
1503
1.20k
      WRITE_UVLC( picHeader->cuQpDeltaSubdivIntra, "ph_cu_qp_delta_subdiv_intra_slice" );
1504
1.20k
    }
1505
1.20k
    if (pps->chromaQpOffsetListLen )
1506
0
    {
1507
0
      WRITE_UVLC( picHeader->cuChromaQpOffsetSubdivIntra, "ph_cu_chroma_qp_offset_subdiv_intra_slice" );
1508
0
    }
1509
1.20k
  }
1510
1511
1.20k
  if (picHeader->picInterSliceAllowed )
1512
0
  {
1513
0
    if (picHeader->splitConsOverride )
1514
0
    {
1515
0
      WRITE_UVLC(floorLog2(picHeader->minQTSize[1]) - sps->log2MinCodingBlockSize, "ph_log2_diff_min_qt_min_cb_inter_slice");
1516
0
      WRITE_UVLC(picHeader->maxMTTDepth[1], "ph_max_mtt_hierarchy_depth_inter_slice");
1517
0
      if (picHeader->maxMTTDepth[1] != 0)
1518
0
      {
1519
0
        WRITE_UVLC(floorLog2(picHeader->maxBTSize[1]) - floorLog2(picHeader->minQTSize[1]), "ph_log2_diff_max_bt_min_qt_inter_slice");
1520
0
        WRITE_UVLC(floorLog2(picHeader->maxTTSize[1]) - floorLog2(picHeader->minQTSize[1]), "ph_log2_diff_max_tt_min_qt_inter_slice");
1521
0
      }
1522
0
    }
1523
1524
    // delta quantization and chrom and chroma offset
1525
0
    if (pps->useDQP)
1526
0
    {
1527
0
      WRITE_UVLC(picHeader->cuQpDeltaSubdivInter, "ph_cu_qp_delta_subdiv_inter_slice");
1528
0
    }
1529
1530
0
    if (pps->chromaQpOffsetListLen )
1531
0
    {
1532
0
      WRITE_UVLC(picHeader->cuChromaQpOffsetSubdivInter, "ph_cu_chroma_qp_offset_subdiv_inter_slice");
1533
0
    }
1534
1535
    // temporal motion vector prediction
1536
0
    if (sps->temporalMVPEnabled)
1537
0
    {
1538
0
      WRITE_FLAG( picHeader->enableTMVP, "ph_temporal_mvp_enabled_flag" );
1539
0
      if (picHeader->enableTMVP && pps->rplInfoInPh)
1540
0
      {
1541
0
        if (picHeader->pRPL[1]->getNumRefEntries() > 0)
1542
0
        {
1543
0
          WRITE_CODE(picHeader->picColFromL0, 1, "ph_collocated_from_l0_flag");
1544
0
        }
1545
0
        if ((picHeader->picColFromL0 && picHeader->pRPL[0]->getNumRefEntries() > 1) ||
1546
0
          (!picHeader->picColFromL0 && picHeader->pRPL[1]->getNumRefEntries() > 1))
1547
0
        {
1548
0
          WRITE_UVLC(picHeader->colRefIdx, "ph_collocated_ref_idx");
1549
0
        }
1550
0
      }
1551
0
    }
1552
1553
  // full-pel MMVD flag
1554
0
    if (sps->fpelMmvd )
1555
0
    {
1556
0
      WRITE_FLAG( picHeader->disFracMMVD, "ph_fpel_mmvd_enabled_flag" );
1557
0
    }
1558
  // mvd L1 zero flag
1559
0
    if (!pps->rplInfoInPh || picHeader->pRPL[1]->getNumRefEntries() > 0)
1560
0
    {
1561
0
      WRITE_FLAG(picHeader->mvdL1Zero, "ph_mvd_l1_zero_flag");
1562
0
    }
1563
1564
  // picture level BDOF disable flags
1565
0
    if (sps->BdofPresent && (!pps->rplInfoInPh || picHeader->pRPL[1]->getNumRefEntries() > 0))
1566
0
    {
1567
0
      WRITE_FLAG(picHeader->disBdofFlag, "ph_disable_bdof_flag");
1568
0
    }
1569
1570
  // picture level DMVR disable flags
1571
0
    if (sps->DmvrPresent && (!pps->rplInfoInPh || picHeader->pRPL[1]->getNumRefEntries() > 0))
1572
0
    {
1573
0
      WRITE_FLAG(picHeader->disDmvrFlag, "ph_disable_dmvr_flag");
1574
0
    }
1575
1576
  // picture level PROF disable flags
1577
0
    if (sps->ProfPresent)
1578
0
    {
1579
0
      WRITE_FLAG(picHeader->disProfFlag, "ph_disable_prof_flag");
1580
0
    }
1581
1582
0
    if ((pps->weightPred || pps->weightedBiPred) && pps->wpInfoInPh )
1583
0
    {
1584
0
      xCodePredWeightTable(picHeader, pps, sps);
1585
0
    }
1586
0
   }
1587
1588
1.20k
  if (pps->qpDeltaInfoInPh)
1589
0
  {
1590
0
    WRITE_SVLC(picHeader->qpDelta, "ph_qp_delta");
1591
0
  }
1592
1593
  // joint Cb/Cr sign flag
1594
1.20k
  if (sps->jointCbCr )
1595
1.20k
  {
1596
1.20k
    WRITE_FLAG( picHeader->jointCbCrSign, "ph_joint_cbcr_sign_flag" );
1597
1.20k
  }
1598
1599
  // sao enable flags
1600
1.20k
  if(sps->saoEnabled)
1601
1.20k
  {
1602
1.20k
    if (pps->saoInfoInPh)
1603
0
    {
1604
0
      WRITE_FLAG(picHeader->saoEnabled[CH_L], "ph_sao_luma_enabled_flag");
1605
0
      if (sps->chromaFormatIdc != CHROMA_400)
1606
0
      {
1607
0
        WRITE_FLAG(picHeader->saoEnabled[CH_C], "ph_sao_chroma_enabled_flag");
1608
0
      }
1609
0
    }
1610
1.20k
  }
1611
1612
  // deblocking filter controls
1613
1.20k
  if (pps->deblockingFilterControlPresent )
1614
0
  {
1615
0
    if(pps->deblockingFilterOverrideEnabled)
1616
0
    {
1617
0
      if (pps->dbfInfoInPh)
1618
0
      {
1619
0
        WRITE_FLAG ( picHeader->deblockingFilterOverride, "ph_deblocking_filter_override_flag" );
1620
 
1621
0
        if(picHeader->deblockingFilterOverride)
1622
0
        {
1623
0
          WRITE_FLAG( picHeader->deblockingFilterDisable, "ph_deblocking_filter_disabled_flag" );
1624
0
          if( !picHeader->deblockingFilterDisable )
1625
0
          {
1626
0
            WRITE_SVLC( picHeader->deblockingFilterBetaOffsetDiv2[COMP_Y], "ph_beta_offset_div2" );
1627
0
            WRITE_SVLC( picHeader->deblockingFilterTcOffsetDiv2[COMP_Y], "ph_tc_offset_div2" );
1628
0
            if( pps->usePPSChromaTool )
1629
0
            {
1630
0
              WRITE_SVLC( picHeader->deblockingFilterBetaOffsetDiv2[COMP_Cb], "ph_cb_beta_offset_div2" );
1631
0
              WRITE_SVLC( picHeader->deblockingFilterTcOffsetDiv2[COMP_Cb], "ph_cb_tc_offset_div2" );
1632
0
              WRITE_SVLC( picHeader->deblockingFilterBetaOffsetDiv2[COMP_Cr], "ph_cr_beta_offset_div2" );
1633
0
              WRITE_SVLC( picHeader->deblockingFilterTcOffsetDiv2[COMP_Cr], "ph_cr_tc_offset_div2" );
1634
0
            }
1635
0
          }
1636
0
        }
1637
0
      }
1638
0
    }
1639
0
  }
1640
1641
  // picture header extension
1642
1.20k
  if(pps->pictureHeaderExtensionPresent)
1643
0
  {
1644
0
    WRITE_UVLC(0,"ph_extension_length");
1645
0
  }
1646
1647
1.20k
  if ( writeRbspTrailingBits )
1648
0
  {
1649
0
    xWriteRbspTrailingBits();
1650
0
  }
1651
1.20k
}
1652
1653
1654
void HLSWriter::codeSliceHeader( const Slice* slice )
1655
1.20k
{
1656
1.20k
  DTRACE( g_trace_ctx, D_HEADER, "=========== Slice ===========\n" );
1657
1658
1.20k
  CodingStructure& cs        = *slice->pic->cs;
1659
1.20k
  const PicHeader *picHeader = cs.picHeader;
1660
1.20k
  const ChromaFormat format  = slice->sps->chromaFormatIdc;
1661
1.20k
  const uint32_t numberValidComponents = getNumberValidComponents(format);
1662
1.20k
  const bool chromaEnabled = isChromaEnabled(format);
1663
1664
1.20k
  WRITE_FLAG(slice->pictureHeaderInSliceHeader, "sh_picture_header_in_slice_header_flag");
1665
1.20k
  if(slice->pictureHeaderInSliceHeader)
1666
1.20k
  {
1667
1.20k
    codePictureHeader(picHeader, false);
1668
1.20k
  }
1669
1670
1.20k
  if (slice->sps->subPicInfoPresent)
1671
0
  {
1672
0
    uint32_t bitsSubPicId;
1673
0
    if (slice->sps->subPicIdMappingExplicitlySignalled)
1674
0
    {
1675
0
      bitsSubPicId = slice->sps->subPicIdLen;
1676
0
    }
1677
0
    else if (slice->pps->subPicIdMappingInPps)
1678
0
    {
1679
0
      bitsSubPicId = slice->pps->subPicIdLen;
1680
0
    }
1681
0
    else
1682
0
    {
1683
0
      bitsSubPicId = ceilLog2(slice->sps->numSubPics);
1684
0
    }
1685
0
    WRITE_CODE(slice->sliceSubPicId, bitsSubPicId, "sh_subpic_id");
1686
0
  }
1687
1688
1.20k
  if (!slice->pps->rectSlice)
1689
0
  {
1690
0
    THROW("no suppport");
1691
0
  }
1692
1.20k
  else
1693
1.20k
  {
1694
    // slice address is the index of the slice within the current sub-picture
1695
1.20k
    uint32_t currSubPicIdx = slice->pps->getSubPicIdxFromSubPicId( slice->sliceSubPicId );
1696
1.20k
    SubPic currSubPic = slice->pps->subPics[currSubPicIdx];
1697
1.20k
    if( currSubPic.numSlicesInSubPic > 1 )
1698
0
    {
1699
0
      int numSlicesInPreviousSubPics = 0;
1700
0
      for(int sp = 0; sp < currSubPicIdx; sp++)
1701
0
      {
1702
0
        numSlicesInPreviousSubPics += slice->pps->subPics[sp].numSlicesInSubPic;
1703
0
      }
1704
0
      int bitsSliceAddress = ceilLog2(currSubPic.numSlicesInSubPic);
1705
0
      WRITE_CODE( slice->sliceMap.sliceID - numSlicesInPreviousSubPics, bitsSliceAddress, "sh_slice_address");
1706
0
    }
1707
1.20k
  }
1708
1709
1.20k
  if (picHeader->picInterSliceAllowed)
1710
0
  {
1711
0
    WRITE_UVLC(slice->sliceType, "sh_slice_type");
1712
0
  }
1713
1.20k
  if (picHeader->gdrOrIrapPic) //th check this
1714
1.20k
  {
1715
1.20k
    WRITE_FLAG(picHeader->noOutputOfPriorPics, "sh_no_output_of_prior_pics_flag");
1716
1.20k
  }
1717
1718
1.20k
  if (!picHeader->picIntraSliceAllowed )
1719
0
  {
1720
0
    CHECK(slice->sliceType == VVENC_I_SLICE, "when pic_intra_slice_allowed_flag = 0, no I_Slice is allowed");
1721
0
  }
1722
1723
1.20k
  if (slice->sps->alfEnabled && !slice->pps->alfInfoInPh)
1724
1.20k
  {
1725
1.20k
    const int alfEnabled = slice->alfEnabled[COMP_Y];
1726
1.20k
    WRITE_FLAG(alfEnabled, "sh_alf_enabled_flag");
1727
1728
1.20k
    if (alfEnabled)
1729
0
    {
1730
0
      WRITE_CODE(slice->numAps, 3, "sh_num_alf_aps_ids_luma");
1731
0
      for (int i = 0; i < slice->numAps; i++)
1732
0
      {
1733
0
        WRITE_CODE(slice->lumaApsId[i], 3, "sh_alf_aps_id_luma");
1734
0
      }
1735
1736
0
      const int alfChromaIdc = slice->alfEnabled[COMP_Cb] + slice->alfEnabled[COMP_Cr] * 2;
1737
0
      if (chromaEnabled)
1738
0
      {
1739
0
        WRITE_FLAG(slice->alfEnabled[COMP_Cb], "sh_alf_cb_enabled_flag");
1740
0
        WRITE_FLAG(slice->alfEnabled[COMP_Cr], "sh_alf_cr_enabled_flag");
1741
0
      }
1742
0
      if (alfChromaIdc)
1743
0
      {
1744
0
        WRITE_CODE(slice->chromaApsId, 3,      "sh_alf_aps_id_chroma");
1745
0
      }
1746
1747
0
      if (slice->sps->ccalfEnabled)
1748
0
      {
1749
0
        WRITE_FLAG(slice->ccAlfCbEnabled,      "sh_cc_alf_cb_enabled_flag");
1750
0
        if( slice->ccAlfCbEnabled )
1751
0
        {
1752
          // write CC ALF Cb APS ID
1753
0
          WRITE_CODE(slice->ccAlfCbApsId, 3,   "sh_cc_alf_cb_aps_id");
1754
0
        }
1755
        // Cr
1756
0
        WRITE_FLAG(slice->ccAlfCrEnabled,      "sh_cc_alf_cr_enabled_flag");
1757
0
        if( slice->ccAlfCrEnabled )
1758
0
        {
1759
          // write CC ALF Cr APS ID
1760
0
          WRITE_CODE(slice->ccAlfCrApsId, 3,   "sh_cc_alf_cr_aps_id");
1761
0
        }
1762
0
      }
1763
0
    }
1764
1.20k
  }
1765
1766
1.20k
  if (picHeader->explicitScalingListEnabled && !slice->pictureHeaderInSliceHeader)
1767
0
  {
1768
0
    WRITE_FLAG(slice->explicitScalingListUsed,  "sh_explicit_scaling_list_used_flag");
1769
0
  }
1770
1771
1.20k
  if(  !slice->pps->rplInfoInPh && (!slice->getIdrPicFlag() || slice->sps->idrRefParamList))
1772
0
  {
1773
0
    int numRPL0 = slice->sps->getNumRPL(0);
1774
    //Write L0 related syntax elements
1775
0
    if (numRPL0 > 0)
1776
0
    {
1777
0
      WRITE_FLAG(slice->rplIdx[0] != -1, "ref_pic_list_sps_flag[0]");
1778
0
    }
1779
0
    if (slice->rplIdx[0] != -1)
1780
0
    {
1781
0
      if (numRPL0 > 1)
1782
0
      {
1783
0
        int numBits = 0;
1784
0
        while ((1 << numBits) < numRPL0)
1785
0
        {
1786
0
          numBits++;
1787
0
        }
1788
0
        WRITE_CODE(slice->rplIdx[0], numBits, "ref_pic_list_idx[0]");
1789
0
      }
1790
0
    }
1791
0
    else
1792
0
    {  //write local RPL0
1793
0
      xCodeRefPicList( slice->rpl[0], slice->sps->longTermRefsPresent, slice->sps->bitsForPOC, !slice->sps->weightPred && !slice->sps->weightedBiPred, -1 );
1794
0
    }
1795
    //Deal POC Msb cycle signalling for LTRP
1796
0
    if (slice->rpl[0]->numberOfLongtermPictures)
1797
0
    {
1798
0
      for (int i = 0; i < slice->rpl[0]->numberOfLongtermPictures + slice->rpl[0]->numberOfShorttermPictures; i++)
1799
0
      {
1800
0
        if (slice->rpl[0]->isLongtermRefPic[i])
1801
0
        {
1802
0
          if (slice->rpl[0]->ltrpInSliceHeader)
1803
0
          {
1804
0
            WRITE_CODE(slice->rpl[0]->refPicIdentifier[i], slice->sps->bitsForPOC, "slice_poc_lsb_lt[listIdx][rplsIdx][j]");
1805
0
          }
1806
0
          WRITE_FLAG(slice->rpl[0]->deltaPocMSBPresent[i], "delta_poc_msb_present_flag[i][j]");
1807
0
          if (slice->rpl[0]->deltaPocMSBPresent[i])
1808
0
          {
1809
0
            WRITE_UVLC(slice->rpl[0]->deltaPocMSBCycleLT[i], "delta_poc_msb_cycle_lt[i][j]");
1810
0
          }
1811
0
        }
1812
0
      }
1813
0
    }
1814
1815
    //Write L1 related syntax elements
1816
0
      if (slice->sps->getNumRPL(1) > 1 && slice->pps->rpl1IdxPresent)
1817
0
      {
1818
0
        WRITE_FLAG(slice->rplIdx[1] != -1 ? 1 : 0, "ref_pic_list_sps_flag[1]");
1819
0
      }
1820
0
      else if (slice->sps->getNumRPL(1) == 0)
1821
0
      {
1822
0
        CHECK(slice->rplIdx[1] != -1, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1823
0
      }
1824
0
      else
1825
0
      {
1826
0
        auto rplsSpsFlag0 = slice->rplIdx[0] != -1 ? 1 : 0;
1827
0
        auto rplsSpsFlag1 = slice->rplIdx[1] != -1 ? 1 : 0;
1828
0
        CHECK(rplsSpsFlag1 != rplsSpsFlag0, "rpl_sps_flag[1] will be infer to 0 and this is not what was expected");
1829
0
      }
1830
1831
0
      if (slice->rplIdx[1] != -1)
1832
0
      {
1833
0
        if (slice->sps->getNumRPL(1) > 1 && slice->pps->rpl1IdxPresent)
1834
0
        {
1835
0
          int numBits = 0;
1836
0
          while ((1 << numBits) < slice->sps->getNumRPL(1))
1837
0
          {
1838
0
            numBits++;
1839
0
          }
1840
0
          WRITE_CODE(slice->rplIdx[1], numBits, "ref_pic_list_idx[1]");
1841
0
        }
1842
0
        else if (slice->sps->getNumRPL(1) == 1)
1843
0
        {
1844
0
          CHECK(slice->rplIdx[1] != 0, "RPL1Idx is not signalled but it is not equal to 0");
1845
0
        }
1846
0
        else
1847
0
        {
1848
0
          CHECK(slice->rplIdx[1] != slice->rplIdx[0], "RPL1Idx is not signalled but it is not the same as RPL0Idx");
1849
0
        }
1850
0
      }
1851
0
      else
1852
0
      {  //write local RPL1
1853
0
        xCodeRefPicList( slice->rpl[1], slice->sps->longTermRefsPresent, slice->sps->bitsForPOC, !(slice->sps->weightPred || slice->sps->weightedBiPred), -1 );
1854
0
      }
1855
      //Deal POC Msb cycle signalling for LTRP
1856
0
      if (slice->rpl[1]->numberOfLongtermPictures)
1857
0
      {
1858
0
        for (int i = 0; i < slice->rpl[1]->numberOfLongtermPictures + slice->rpl[1]->numberOfShorttermPictures; i++)
1859
0
        {
1860
0
          if (slice->rpl[1]->isLongtermRefPic[i])
1861
0
          {
1862
0
            if (slice->rpl[1]->ltrpInSliceHeader)
1863
0
            {
1864
0
              WRITE_CODE(slice->rpl[1]->refPicIdentifier[i], slice->sps->bitsForPOC,
1865
0
                         "slice_poc_lsb_lt[listIdx][rplsIdx][j]");
1866
0
            }
1867
0
            WRITE_FLAG(slice->rpl[1]->deltaPocMSBPresent[i], "delta_poc_msb_present_flag[i][j]");
1868
0
            if (slice->rpl[1]->deltaPocMSBPresent[i])
1869
0
            {
1870
0
              WRITE_UVLC(slice->rpl[1]->deltaPocMSBCycleLT[i], "delta_poc_msb_cycle_lt[i][j]");
1871
0
            }
1872
0
          }
1873
0
        }
1874
0
      }
1875
0
    }
1876
1877
    //check if numrefidxes match the defaults. If not, override
1878
1879
1.20k
    if ((!slice->isIntra() && slice->rpl[0]->getNumRefEntries() > 1) ||
1880
1.20k
        (slice->isInterB() && slice->rpl[1]->getNumRefEntries() > 1) )
1881
0
    {
1882
0
      int defaultL0 = std::min<int>(slice->rpl[0]->getNumRefEntries(), slice->pps->numRefIdxL0DefaultActive);
1883
0
      int defaultL1 = slice->isInterB() ? std::min<int>(slice->rpl[1]->getNumRefEntries(), slice->pps->numRefIdxL1DefaultActive) : 0;
1884
0
      bool overrideFlag = ( slice->numRefIdx[ REF_PIC_LIST_0 ] != defaultL0 || ( slice->isInterB() && slice->numRefIdx[ REF_PIC_LIST_1 ] != defaultL1 ) );
1885
0
      WRITE_FLAG( overrideFlag ? 1 : 0, "num_ref_idx_active_override_flag" );
1886
0
      if( overrideFlag )
1887
0
      {
1888
0
        if(slice->rpl[0]->getNumRefEntries() > 1)
1889
0
        {
1890
0
          WRITE_UVLC( slice->numRefIdx[ REF_PIC_LIST_0 ] - 1, "num_ref_idx_l0_active_minus1" );
1891
0
        }
1892
1893
0
        if( slice->isInterB() && slice->rpl[1]->getNumRefEntries() > 1)
1894
0
        {
1895
0
          WRITE_UVLC( slice->numRefIdx[ REF_PIC_LIST_1 ] - 1, "num_ref_idx_l1_active_minus1" );
1896
0
        }
1897
0
      }
1898
0
    }
1899
1900
1.20k
    if( !slice->isIntra() )
1901
0
    {
1902
0
      if( !slice->isIntra() && slice->pps->cabacInitPresent )
1903
0
      {
1904
0
        const SliceType encCABACTableIdx = slice->encCABACTableIdx;
1905
0
        bool encCabacInitFlag = ( slice->sliceType != encCABACTableIdx && encCABACTableIdx != VVENC_I_SLICE ) ? true : false;
1906
0
        WRITE_FLAG( encCabacInitFlag ? 1 : 0, "sh_cabac_init_flag" );
1907
0
      }
1908
0
    }
1909
1910
1.20k
    if( slice->picHeader->enableTMVP  && !slice->pps->rplInfoInPh)
1911
0
    {
1912
0
      if(!slice->pps->rplInfoInPh)
1913
0
      {
1914
0
        if (slice->sliceType == VVENC_B_SLICE)
1915
0
        {
1916
0
          WRITE_FLAG(slice->colFromL0Flag, "sh_collocated_from_l0_flag");
1917
0
        }
1918
0
      }
1919
1920
0
    if( slice->sliceType != VVENC_I_SLICE &&
1921
0
      ( ( slice->colFromL0Flag == 1 && slice->numRefIdx[ REF_PIC_LIST_0 ] > 1 ) ||
1922
0
        ( slice->colFromL0Flag == 0 && slice->numRefIdx[ REF_PIC_LIST_1 ] > 1 ) ) )
1923
0
    {
1924
0
      WRITE_UVLC( slice->colRefIdx, "sh_collocated_ref_idx" );
1925
0
    }
1926
0
  }
1927
1928
1.20k
  if( ( slice->pps->weightPred && slice->sliceType == VVENC_P_SLICE ) || ( slice->pps->weightedBiPred && slice->sliceType == VVENC_B_SLICE ) )
1929
0
  {
1930
0
    if( !slice->pps->wpInfoInPh )
1931
0
    {
1932
0
      xCodePredWeightTable( slice );
1933
0
    }
1934
0
  }
1935
1936
1.20k
  if (!slice->pps->qpDeltaInfoInPh)
1937
1.20k
  {
1938
1.20k
    WRITE_SVLC(slice->sliceQp - (slice->pps->picInitQPMinus26 + 26), "slice_qp_delta");
1939
1.20k
  }
1940
1.20k
  if (slice->pps->sliceChromaQpFlag)
1941
1.20k
  {
1942
1.20k
    if (numberValidComponents > COMP_Cb)
1943
1.20k
    {
1944
1.20k
      WRITE_SVLC( slice->sliceChromaQpDelta[COMP_Cb], "sh_cb_qp_offset" );
1945
1.20k
    }
1946
1.20k
    if (numberValidComponents > COMP_Cr)
1947
1.20k
    {
1948
1.20k
      WRITE_SVLC( slice->sliceChromaQpDelta[COMP_Cr], "sh_cr_qp_offset" );
1949
1.20k
      if (slice->sps->jointCbCr)
1950
1.20k
      {
1951
1.20k
        WRITE_SVLC( slice->sliceChromaQpDelta[COMP_JOINT_CbCr], "sh_joint_cbcr_qp_offset");
1952
1.20k
      }
1953
1.20k
    }
1954
1.20k
    CHECK(numberValidComponents < COMP_Cr+1, "Too many valid components");
1955
1.20k
  }
1956
1957
1.20k
  if (slice->pps->chromaQpOffsetListLen>0)
1958
0
  {
1959
0
    WRITE_FLAG(slice->chromaQpAdjEnabled, "sh_cu_chroma_qp_offset_enabled_flag");
1960
0
  }
1961
1962
1.20k
  if( slice->sps->saoEnabled && !slice->pps->saoInfoInPh )
1963
1.20k
  {
1964
1.20k
    WRITE_FLAG( slice->saoEnabled[CH_L], "sh_sao_luma_flag" );
1965
1.20k
    if( chromaEnabled )
1966
1.20k
    {
1967
1.20k
      WRITE_FLAG( slice->saoEnabled[CH_C], "sh_sao_chroma_flag" );
1968
1.20k
    }
1969
1.20k
  }
1970
1971
1972
1.20k
  if (slice->pps->deblockingFilterControlPresent && !slice->pps->dbfInfoInPh)
1973
0
  {
1974
0
    if (slice->pps->deblockingFilterOverrideEnabled )
1975
0
    {
1976
0
      WRITE_FLAG(slice->deblockingFilterOverride, "sh_deblocking_params_present_flag");
1977
0
    }
1978
0
    if (slice->deblockingFilterOverride)
1979
0
    {
1980
0
      if (!slice->pps->deblockingFilterDisabled)
1981
0
      {
1982
0
       WRITE_FLAG(slice->deblockingFilterDisable, "sh_deblocking_filter_disabled_flag");
1983
0
      }
1984
0
      if(!slice->deblockingFilterDisable)
1985
0
      {
1986
0
        WRITE_SVLC (slice->deblockingFilterBetaOffsetDiv2[COMP_Y],   "slice_beta_offset_div2");
1987
0
        WRITE_SVLC (slice->deblockingFilterTcOffsetDiv2[COMP_Y],     "slice_tc_offset_div2");
1988
0
        if( slice->pps->usePPSChromaTool )
1989
0
        {
1990
0
          WRITE_SVLC (slice->deblockingFilterBetaOffsetDiv2[COMP_Cb], "slice_cb_beta_offset_div2");
1991
0
          WRITE_SVLC (slice->deblockingFilterTcOffsetDiv2[COMP_Cb],   "slice_cb_tc_offset_div2");
1992
0
          WRITE_SVLC (slice->deblockingFilterBetaOffsetDiv2[COMP_Cr], "slice_cr_beta_offset_div2");
1993
0
          WRITE_SVLC (slice->deblockingFilterTcOffsetDiv2[COMP_Cr],   "slice_cr_tc_offset_div2");
1994
0
        }
1995
0
      }
1996
0
    }
1997
0
  }
1998
1999
  // dependent quantization
2000
1.20k
  if( slice->sps->depQuantEnabled )
2001
1.20k
  {
2002
1.20k
    WRITE_FLAG(slice->depQuantEnabled, "sh_dep_quant_used_flag");
2003
1.20k
  }
2004
2005
  // sign data hiding
2006
1.20k
  if( slice->sps->signDataHidingEnabled && !slice->depQuantEnabled )
2007
0
  {
2008
0
    WRITE_FLAG(slice->signDataHidingEnabled, "sh_sign_data_hiding_used_flag" );
2009
0
  }
2010
1.20k
  if( slice->sps->transformSkip && !slice->depQuantEnabled && !slice->signDataHidingEnabled )
2011
0
  {
2012
0
    WRITE_FLAG(slice->tsResidualCodingDisabled, "sh_ts_residual_coding_disabled_flag");
2013
0
  }
2014
2015
1.20k
  if(slice->pps->sliceHeaderExtensionPresent)
2016
0
  {
2017
0
    WRITE_UVLC(0,"slice_header_extension_length");
2018
0
  }
2019
1.20k
}
2020
2021
void  HLSWriter::codeConstraintInfo  ( const ConstraintInfo* cinfo )
2022
1.20k
{
2023
1.20k
  WRITE_FLAG(cinfo->gciPresent,                         "gci_present_flag");
2024
1.20k
  if (cinfo->gciPresent)
2025
0
  {
2026
0
    WRITE_FLAG(cinfo->intraOnlyConstraintFlag,              "gci_intra_only_constraint_flag");
2027
0
    WRITE_FLAG(cinfo->allLayersIndependentConstraintFlag,   "gci_all_layers_independent_constraint_flag");
2028
0
    WRITE_FLAG(cinfo->onePictureOnlyConstraintFlag,         "gci_one_au_only_constraint_flag");
2029
2030
    /* picture format */
2031
0
    WRITE_CODE(16 - cinfo->maxBitDepthConstraintIdc, 4,     "gci_sixteen_minus_max_bitdepth_constraint_idc");
2032
0
    WRITE_CODE(3 - cinfo->maxChromaFormatConstraintIdc, 2,  "gci_three_minus_max_chroma_format_constraint_idc");
2033
2034
    /* NAL unit type related */
2035
0
    WRITE_FLAG(cinfo->noMixedNaluTypesInPicConstraintFlag,  "gci_no_mixed_nalu_types_in_pic_constraint_flag");
2036
0
    WRITE_FLAG(cinfo->noTrailConstraintFlag,                "gci_no_trail_constraint_flag");
2037
0
    WRITE_FLAG(cinfo->noStsaConstraintFlag,                 "gci_no_stsa_constraint_flag");
2038
0
    WRITE_FLAG(cinfo->noRaslConstraintFlag,                 "gci_no_rasl_constraint_flag");
2039
0
    WRITE_FLAG(cinfo->noRadlConstraintFlag,                 "gci_no_radl_constraint_flag");
2040
0
    WRITE_FLAG(cinfo->noIdrConstraintFlag,                  "gci_no_idr_constraint_flag");
2041
0
    WRITE_FLAG(cinfo->noCraConstraintFlag,                  "gci_no_cra_constraint_flag");
2042
0
    WRITE_FLAG(cinfo->noGdrConstraintFlag,                  "gci_no_gdr_constraint_flag");
2043
0
    WRITE_FLAG(cinfo->noApsConstraintFlag,                  "gci_no_aps_constraint_flag");
2044
0
    WRITE_FLAG(cinfo->noIdrRplConstraintFlag,               "gci_no_idr_rpl_constraint_flag");
2045
2046
    /* tile, slice, subpicture partitioning */
2047
0
    WRITE_FLAG(cinfo->oneTilePerPicConstraintFlag,          "gci_one_tile_per_pic_constraint_flag");
2048
0
    WRITE_FLAG(cinfo->picHeaderInSliceHeaderConstraintFlag, "gci_pic_header_in_slice_header_constraint_flag");
2049
0
    WRITE_FLAG(cinfo->oneSlicePerPicConstraintFlag,         "gci_one_slice_per_pic_constraint_flag");
2050
0
    WRITE_FLAG(cinfo->noRectSliceConstraintFlag,            "gci_no_rectangular_slice_constraint_flag");
2051
0
    WRITE_FLAG(cinfo->oneSlicePerSubpicConstraintFlag,      "gci_one_slice_per_subpic_constraint_flag");
2052
0
    WRITE_FLAG(cinfo->noSubpicInfoConstraintFlag,           "gci_no_subpic_info_constraint_flag");
2053
2054
2055
    /* CTU and block partitioning */
2056
0
    WRITE_CODE(3 - (cinfo->maxLog2CtuSizeConstraintIdc - 5), 2, "gci_three_minus_max_log2_ctu_size_constraint_idc");
2057
0
    WRITE_FLAG(cinfo->noPartitionConstraintsOverrideConstraintFlag, "gci_no_partition_constraints_override_constraint_flag");
2058
0
    WRITE_FLAG(cinfo->noMttConstraintFlag,                  "gci_no_mtt_constraint_flag");
2059
0
    WRITE_FLAG(cinfo->noQtbttDualTreeIntraConstraintFlag,   "gci_no_qtbtt_dual_tree_intra_constraint_flag");
2060
2061
    /* intra */
2062
0
    WRITE_FLAG(cinfo->noPaletteConstraintFlag,              "gci_no_palette_constraint_flag");
2063
0
    WRITE_FLAG(cinfo->noIbcConstraintFlag,                  "gci_no_ibc_constraint_flag");
2064
0
    WRITE_FLAG(cinfo->noIspConstraintFlag,                  "gci_no_isp_constraint_flag");
2065
0
    WRITE_FLAG(cinfo->noMrlConstraintFlag,                  "gci_no_mrl_constraint_flag");
2066
0
    WRITE_FLAG(cinfo->noMipConstraintFlag,                  "gci_no_mip_constraint_flag");
2067
0
    WRITE_FLAG(cinfo->noCclmConstraintFlag,                 "gci_no_cclm_constraint_flag");
2068
2069
    /* inter */
2070
0
    WRITE_FLAG(cinfo->noRprConstraintFlag,                  "gci_no_ref_pic_resampling_constraint_flag");
2071
0
    WRITE_FLAG(cinfo->noResChangeInClvsConstraintFlag,      "gci_no_res_change_in_clvs_constraint_flag");
2072
0
    WRITE_FLAG(cinfo->noWeightedPredictionConstraintFlag,   "gci_no_weighted_prediction_constraint_flag");
2073
0
    WRITE_FLAG(cinfo->noRefWraparoundConstraintFlag,        "gci_no_ref_wraparound_constraint_flag");
2074
0
    WRITE_FLAG(cinfo->noTemporalMvpConstraintFlag,          "gci_no_temporal_mvp_constraint_flag");
2075
0
    WRITE_FLAG(cinfo->noSbtmvpConstraintFlag,               "gci_no_sbtmvp_constraint_flag");
2076
0
    WRITE_FLAG(cinfo->noAmvrConstraintFlag,                 "gci_no_amvr_constraint_flag");
2077
0
    WRITE_FLAG(cinfo->noBdofConstraintFlag,                 "gci_no_bdof_constraint_flag");
2078
0
    WRITE_FLAG(cinfo->noSmvdConstraintFlag,                 "gci_no_smvd_constraint_flag");
2079
0
    WRITE_FLAG(cinfo->noDmvrConstraintFlag,                 "gci_no_dmvr_constraint_flag");
2080
0
    WRITE_FLAG(cinfo->noMmvdConstraintFlag,                 "gci_no_mmvd_constraint_flag");
2081
0
    WRITE_FLAG(cinfo->noAffineMotionConstraintFlag,         "gci_no_affine_motion_constraint_flag");
2082
0
    WRITE_FLAG(cinfo->noProfConstraintFlag,                 "gci_no_prof_constraint_flag");
2083
0
    WRITE_FLAG(cinfo->noBcwConstraintFlag,                  "gci_no_bcw_constraint_flag");
2084
0
    WRITE_FLAG(cinfo->noCiipConstraintFlag,                 "gci_no_ciip_constraint_flag");
2085
0
    WRITE_FLAG(cinfo->noGeoConstraintFlag,                  "gci_no_gpm_constraint_flag");
2086
2087
    /* transform, quantization, residual */
2088
0
    WRITE_FLAG(cinfo->noLumaTransformSize64ConstraintFlag,  "gci_no_luma_transform_size_64_constraint_flag");
2089
0
    WRITE_FLAG(cinfo->noTransformSkipConstraintFlag,        "gci_no_transform_skip_constraint_flag");
2090
0
    WRITE_FLAG(cinfo->noBDPCMConstraintFlag,                "gci_no_bdpcm_constraint_flag");
2091
0
    WRITE_FLAG(cinfo->noMtsConstraintFlag,                  "gci_no_mts_constraint_flag");
2092
0
    WRITE_FLAG(cinfo->noLfnstConstraintFlag,                "gci_no_lfnst_constraint_flag");
2093
0
    WRITE_FLAG(cinfo->noJointCbCrConstraintFlag,            "gci_no_joint_cbcr_constraint_flag");
2094
0
    WRITE_FLAG(cinfo->noSbtConstraintFlag,                  "gci_no_sbt_constraint_flag");
2095
0
    WRITE_FLAG(cinfo->noActConstraintFlag,                  "gci_no_act_constraint_flag");
2096
0
    WRITE_FLAG(cinfo->noExplicitScaleListConstraintFlag,    "gci_no_explicit_scaling_list_constraint_flag");
2097
0
    WRITE_FLAG(cinfo->noDepQuantConstraintFlag,             "gci_no_dep_quant_constraint_flag");
2098
0
    WRITE_FLAG(cinfo->noSignDataHidingConstraintFlag,       "gci_no_sign_data_hiding_constraint_flag");
2099
0
    WRITE_FLAG(cinfo->noQpDeltaConstraintFlag,              "gci_no_qp_delta_constraint_flag");
2100
0
    WRITE_FLAG(cinfo->noChromaQpOffsetConstraintFlag,       "gci_no_chroma_qp_offset_constraint_flag");
2101
2102
    /* loop filter */
2103
0
    WRITE_FLAG(cinfo->noSaoConstraintFlag,                  "gci_no_sao_constraint_flag");
2104
0
    WRITE_FLAG(cinfo->noAlfConstraintFlag,                  "gci_no_alf_constraint_flag");
2105
0
    WRITE_FLAG(cinfo->noCCAlfConstraintFlag,                "gci_no_ccalf_constraint_flag");
2106
0
    WRITE_FLAG(cinfo->noLmcsConstraintFlag,                 "gci_no_lmcs_constraint_flag");
2107
0
    WRITE_FLAG(cinfo->noLadfConstraintFlag,                 "gci_no_ladf_constraint_flag");
2108
0
    WRITE_FLAG(cinfo->noVirtualBoundaryConstraintFlag,      "gci_no_virtual_boundaries_constraint_flag");
2109
2110
    //The value of gci_num_reserved_bits shall be equal to 0 in bitstreams conforming to this version of this Specification.
2111
    //Other values of gci_num_reserved_bits are reserved for future use by ITU-T | ISO/IEC.
2112
0
    WRITE_CODE(0, 8,                                        "gci_num_reserved_bits");
2113
0
  }
2114
2115
7.20k
  while (!isByteAligned())
2116
6.00k
  {
2117
6.00k
    WRITE_FLAG(0, "gci_alignment_zero_bit");
2118
6.00k
  }
2119
1.20k
}
2120
2121
2122
void  HLSWriter::codeProfileTierLevel    ( const ProfileTierLevel* ptl, bool profileTierPresent, int maxNumSubLayersMinus1 )
2123
1.20k
{
2124
1.20k
  if(profileTierPresent)
2125
1.20k
  {
2126
1.20k
    WRITE_CODE( (uint32_t)ptl->profileIdc, 7 ,        "general_profile_idc"                     );
2127
1.20k
    WRITE_FLAG( ptl->tierFlag==vvencTier::VVENC_TIER_HIGH,           "general_tier_flag"                       );
2128
1.20k
  }
2129
2130
1.20k
  WRITE_CODE( (uint32_t)ptl->levelIdc, 8 ,            "general_level_idc");
2131
1.20k
  WRITE_FLAG( ptl->frameOnlyConstraintFlag,           "ptl_frame_only_constraint_flag" );
2132
1.20k
  WRITE_FLAG( ptl->multiLayerEnabledFlag,             "ptl_multilayer_enabled_flag"    );
2133
1.20k
  if(profileTierPresent)
2134
1.20k
  {
2135
1.20k
    codeConstraintInfo( &ptl->constraintInfo );
2136
1.20k
  }
2137
2138
7.20k
  for (int i = maxNumSubLayersMinus1 - 1; i >= 0; i--)
2139
6.00k
  {
2140
6.00k
    WRITE_FLAG( ptl->subLayerLevelPresent[i],         "sub_layer_level_present_flag[i]" );
2141
6.00k
  }
2142
2143
4.80k
  while (!isByteAligned())
2144
3.60k
  {
2145
3.60k
    WRITE_FLAG(0,                                     "ptl_reserved_zero_bit");
2146
3.60k
  }
2147
2148
7.20k
  for (int i = maxNumSubLayersMinus1 - 1; i >= 0; i--)
2149
6.00k
  {
2150
6.00k
    if( ptl->subLayerLevelPresent[i] )
2151
0
    {
2152
0
      WRITE_CODE( (uint32_t)ptl->subLayerLevelIdc[i], 8, "sub_layer_level_idc[i]" );
2153
0
    }
2154
6.00k
  }
2155
2156
1.20k
  if (profileTierPresent)
2157
1.20k
  {
2158
1.20k
    WRITE_CODE(ptl->numSubProfile, 8, "ptl_num_sub_profiles");
2159
1.20k
    for (int i = 0; i < ptl->numSubProfile; i++)
2160
0
    {
2161
0
      WRITE_CODE(ptl->subProfileIdc[i], 32, "general_sub_profile_idc[i]");
2162
0
    }
2163
1.20k
  }
2164
1.20k
}
2165
2166
2167
/**
2168
* Write tiles and wavefront substreams sizes for the slice header (entry points).
2169
*
2170
* \param pSlice Slice structure that contains the substream size information.
2171
*/
2172
void  HLSWriter::codeTilesWPPEntryPoint( Slice* pSlice )
2173
1.20k
{
2174
1.20k
  int numEntryPoints = pSlice->getNumEntryPoints( *pSlice->sps, *pSlice->pps );
2175
1.20k
  if( numEntryPoints == 0 )
2176
1.20k
  {
2177
1.20k
    return;
2178
1.20k
  }
2179
2180
0
  uint32_t maxOffset = 0;
2181
0
  for(int idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2182
0
  {
2183
0
    uint32_t offset=pSlice->getSubstreamSize(idx);
2184
0
    if ( offset > maxOffset )
2185
0
    {
2186
0
      maxOffset = offset;
2187
0
    }
2188
0
  }
2189
2190
  // Determine number of bits "offsetLenMinus1+1" required for entry point information
2191
0
  uint32_t offsetLenMinus1 = 0;
2192
0
  while (maxOffset >= (1u << (offsetLenMinus1 + 1)))
2193
0
  {
2194
0
    offsetLenMinus1++;
2195
0
    CHECK(offsetLenMinus1 + 1 >= 32, "Invalid offset length minus 1");
2196
0
  }
2197
2198
0
  if (pSlice->getNumberOfSubstreamSizes()>0)
2199
0
  {
2200
0
    WRITE_UVLC(offsetLenMinus1, "sh_entry_offset_len_minus1");
2201
2202
0
    for (uint32_t idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2203
0
    {
2204
0
      WRITE_CODE(pSlice->getSubstreamSize(idx)-1, offsetLenMinus1+1, "sh_entry_point_offset_minus1");
2205
0
    }
2206
0
  }
2207
0
}
2208
2209
2210
// ====================================================================================================================
2211
// Protected member functions
2212
// ====================================================================================================================
2213
2214
//! Code weighted prediction tables
2215
void HLSWriter::xCodePredWeightTable( const Slice* slice )
2216
0
{
2217
0
  WPScalingParam      *wp;
2218
0
  const ChromaFormat  format                = slice->sps->chromaFormatIdc;
2219
0
  const uint32_t      numberValidComponents = getNumberValidComponents(format);
2220
0
  const bool          bChroma               = isChromaEnabled(format);
2221
0
  const int           iNbRef                = (slice->sliceType == VVENC_B_SLICE ) ? (2) : (1);
2222
0
  bool                bDenomCoded           = false;
2223
0
  uint32_t            uiTotalSignalledWeightFlags = 0;
2224
2225
0
  if ( (slice->sliceType==VVENC_P_SLICE && slice->pps->weightPred) || (slice->sliceType==VVENC_B_SLICE && slice->pps->weightedBiPred) )
2226
0
  {
2227
0
    for ( int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
2228
0
    {
2229
0
      RefPicList  refPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2230
2231
      // NOTE: wp[].log2WeightDenom and wp[].presentFlag are actually per-channel-type settings.
2232
2233
0
      for ( int iRefIdx=0 ; iRefIdx<slice->numRefIdx[ refPicList ] ; iRefIdx++ )
2234
0
      {
2235
0
        slice->getWpScaling(refPicList, iRefIdx, wp);
2236
0
        if ( !bDenomCoded )
2237
0
        {
2238
0
          int iDeltaDenom;
2239
0
          WRITE_UVLC( wp[COMP_Y].log2WeightDenom, "luma_log2_weight_denom" );
2240
2241
0
          if( bChroma )
2242
0
          {
2243
0
            CHECK( wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported" );
2244
0
            iDeltaDenom = (wp[COMP_Cb].log2WeightDenom - wp[COMP_Y].log2WeightDenom);
2245
0
            WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
2246
0
          }
2247
0
          bDenomCoded = true;
2248
0
        }
2249
0
        WRITE_FLAG( wp[COMP_Y].presentFlag, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
2250
0
        uiTotalSignalledWeightFlags += wp[COMP_Y].presentFlag;
2251
0
      }
2252
0
      if (bChroma)
2253
0
      {
2254
0
        for ( int iRefIdx=0 ; iRefIdx<slice->numRefIdx[ refPicList ] ; iRefIdx++ )
2255
0
        {
2256
0
          slice->getWpScaling( refPicList, iRefIdx, wp );
2257
0
          CHECK( wp[COMP_Cb].presentFlag != wp[COMP_Cr].presentFlag, "Inconsistent settings for chroma channels" );
2258
0
          WRITE_FLAG( wp[COMP_Cb].presentFlag, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
2259
0
          uiTotalSignalledWeightFlags += 2*wp[COMP_Cb].presentFlag;
2260
0
        }
2261
0
      }
2262
2263
0
      for ( int iRefIdx=0 ; iRefIdx<slice->numRefIdx[ refPicList ] ; iRefIdx++ )
2264
0
      {
2265
0
        slice->getWpScaling(refPicList, iRefIdx, wp);
2266
0
        if ( wp[COMP_Y].presentFlag )
2267
0
        {
2268
0
          int iDeltaWeight = (wp[COMP_Y].iWeight - (1<<wp[COMP_Y].log2WeightDenom));
2269
0
          WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
2270
0
          WRITE_SVLC( wp[COMP_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
2271
0
        }
2272
2273
0
        if ( bChroma )
2274
0
        {
2275
0
          if ( wp[COMP_Cb].presentFlag )
2276
0
          {
2277
0
            for ( int j = COMP_Cb ; j < numberValidComponents ; j++ )
2278
0
            {
2279
0
              CHECK(wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported");
2280
0
              int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMP_Cb].log2WeightDenom));
2281
0
              WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
2282
2283
0
              int range=128;
2284
0
              int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].log2WeightDenom) ) );
2285
0
              int iDeltaChroma = (wp[j].iOffset - pred);
2286
0
              WRITE_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
2287
0
            }
2288
0
          }
2289
0
        }
2290
0
      }
2291
0
    }
2292
0
    CHECK(uiTotalSignalledWeightFlags>24, "Too many signalled weight flags");
2293
0
  }
2294
0
}
2295
2296
2297
void HLSWriter::xCodePredWeightTable( const PicHeader *picHeader, const PPS *pps, const SPS *sps )
2298
0
{
2299
0
  WPScalingParam  *wp;
2300
0
  const ChromaFormat  format                = sps->chromaFormatIdc;
2301
0
  const uint32_t      numberValidComponents = getNumberValidComponents(format);
2302
0
  const bool          bChroma               = isChromaEnabled(format);
2303
0
  bool                bDenomCoded           = false;
2304
0
  uint32_t            uiTotalSignalledWeightFlags = 0;
2305
0
  uint32_t            numLxWeights                = picHeader->numL0Weights;
2306
0
  bool                moreSyntaxToBeParsed        = true;
2307
0
  for (int iNumRef = 0; iNumRef < NUM_REF_PIC_LIST_01 && moreSyntaxToBeParsed; iNumRef++)   // loop over l0 and l1 syntax elements
2308
0
  {
2309
0
    RefPicList  refPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2310
2311
    // NOTE: wp[].log2WeightDenom and wp[].presentFlag are actually per-channel-type settings.
2312
2313
0
    for ( int iRefIdx=0 ; iRefIdx<numLxWeights ; iRefIdx++ )
2314
0
    {
2315
0
      picHeader->getWpScaling(refPicList, iRefIdx, wp);
2316
0
      if ( !bDenomCoded )
2317
0
      {
2318
0
        int iDeltaDenom;
2319
0
        WRITE_UVLC( wp[COMP_Y].log2WeightDenom, "luma_log2_weight_denom" );
2320
2321
0
        if( bChroma )
2322
0
        {
2323
0
          CHECK( wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported" );
2324
0
          iDeltaDenom = (wp[COMP_Cb].log2WeightDenom - wp[COMP_Y].log2WeightDenom);
2325
0
          WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
2326
0
        }
2327
0
        bDenomCoded = true;
2328
0
      }
2329
0
      WRITE_FLAG( wp[COMP_Y].presentFlag, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
2330
0
      uiTotalSignalledWeightFlags += wp[COMP_Y].presentFlag;
2331
0
    }
2332
0
    if (bChroma)
2333
0
    {
2334
0
      for ( int iRefIdx=0 ; iRefIdx<numLxWeights; iRefIdx++ )
2335
0
      {
2336
0
        picHeader->getWpScaling( refPicList, iRefIdx, wp );
2337
0
        CHECK( wp[COMP_Cb].presentFlag != wp[COMP_Cr].presentFlag, "Inconsistent settings for chroma channels" );
2338
0
        WRITE_FLAG( wp[COMP_Cb].presentFlag, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
2339
0
        uiTotalSignalledWeightFlags += 2*wp[COMP_Cb].presentFlag;
2340
0
      }
2341
0
    }
2342
2343
0
    for ( int iRefIdx=0 ; iRefIdx<numLxWeights; iRefIdx++ )
2344
0
    {
2345
0
      picHeader->getWpScaling(refPicList, iRefIdx, wp);
2346
0
      if ( wp[COMP_Y].presentFlag )
2347
0
      {
2348
0
        int iDeltaWeight = (wp[COMP_Y].iWeight - (1<<wp[COMP_Y].log2WeightDenom));
2349
0
        WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
2350
0
        WRITE_SVLC( wp[COMP_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
2351
0
      }
2352
2353
0
      if ( bChroma )
2354
0
      {
2355
0
        if ( wp[COMP_Cb].presentFlag )
2356
0
        {
2357
0
          for ( int j = COMP_Cb ; j < numberValidComponents ; j++ )
2358
0
          {
2359
0
            CHECK(wp[COMP_Cb].log2WeightDenom != wp[COMP_Cr].log2WeightDenom, "Chroma blocks of different size not supported");
2360
0
            int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMP_Cb].log2WeightDenom));
2361
0
            WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
2362
2363
0
            int range=128;
2364
0
            int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].log2WeightDenom) ) );
2365
0
            int iDeltaChroma = (wp[j].iOffset - pred);
2366
0
            WRITE_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
2367
0
          }
2368
0
        }
2369
0
      }
2370
0
    }
2371
0
    if (iNumRef == 0 )
2372
0
    {
2373
0
      numLxWeights = picHeader->numL1Weights;
2374
0
      if (pps->weightedBiPred == 0) 
2375
0
      {
2376
0
        numLxWeights = 0;
2377
0
      }
2378
0
      else if (picHeader->pRPL[1]->getNumRefEntries() > 0)
2379
0
      {
2380
0
        WRITE_UVLC(numLxWeights, "num_l1_weights");
2381
0
      }
2382
0
      moreSyntaxToBeParsed = (numLxWeights == 0) ? false : true;
2383
0
    }
2384
0
  }
2385
0
  CHECK(uiTotalSignalledWeightFlags>24, "Too many signalled weight flags");
2386
0
}
2387
2388
void HLSWriter::alfFilter( const AlfParam& alfParam, const bool isChroma, const int altIdx )
2389
0
{
2390
0
  AlfFilterShape alfShape(isChroma ? 5 : 7);
2391
0
  const short* coeff = isChroma ? alfParam.chromaCoeff[altIdx] : alfParam.lumaCoeff;
2392
0
  const short* clipp = isChroma ? alfParam.chromaClipp[altIdx] : alfParam.lumaClipp;
2393
0
  const int numFilters = isChroma ? 1 : alfParam.numLumaFilters;
2394
2395
  // vlc for all
2396
2397
  // Filter coefficients
2398
0
  for( int ind = 0; ind < numFilters; ++ind )
2399
0
  {
2400
0
    for( int i = 0; i < alfShape.numCoeff - 1; i++ )
2401
0
    {
2402
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]
2403
0
      if( abs( coeff[ ind* MAX_NUM_ALF_LUMA_COEFF + i ] ) != 0 )
2404
0
      {
2405
0
        WRITE_FLAG( ( coeff[ ind* MAX_NUM_ALF_LUMA_COEFF + i ] < 0 ) ? 1 : 0, isChroma ? "alf_chroma_coeff_sign" : "alf_luma_coeff_sign" );
2406
0
      }
2407
0
    }
2408
0
  }
2409
2410
  // Clipping values coding
2411
0
  if( alfParam.nonLinearFlag[isChroma] )
2412
0
  {
2413
0
    for (int ind = 0; ind < numFilters; ++ind)
2414
0
    {
2415
0
      for (int i = 0; i < alfShape.numCoeff - 1; i++)
2416
0
      {
2417
0
        WRITE_CODE(clipp[ind* MAX_NUM_ALF_LUMA_COEFF + i], 2, isChroma ? "alf_chroma_clip_idx" : "alf_luma_clip_idx");
2418
0
      }
2419
0
    }
2420
0
  }
2421
0
}
2422
2423
} // namespace vvenc
2424
2425
//! \}
2426