Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvdec/source/Lib/DecoderLib/SEIread.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) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC 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     SEIread.cpp
45
 \brief    reading funtionality for SEI messages
46
 */
47
48
#include "CommonLib/CommonDef.h"
49
#include "CommonLib/BitStream.h"
50
#include "CommonLib/Slice.h"
51
#include "VLCReader.h"
52
#include "SEIread.h"
53
#include "CommonLib/Picture.h"
54
#include "CommonLib/dtrace_next.h"
55
#include <iomanip>
56
57
#include "vvdec/sei.h"
58
59
namespace vvdec
60
{
61
62
#if ENABLE_TRACING
63
void  xTraceSEIHeader()
64
{
65
  DTRACE( g_trace_ctx, D_HEADER, "=========== SEI message ===========\n");
66
}
67
68
void  xTraceSEIMessageType(vvdecSEIPayloadType payloadType)
69
{
70
  DTRACE( g_trace_ctx, D_HEADER, "=========== %s SEI message ===========\n", SEI_internal::getSEIMessageString( payloadType ) );
71
}
72
#endif
73
74
void SEIReader::sei_read_scode(std::ostream *pOS, uint32_t length, int& code, const char *pSymbolName)
75
0
{
76
0
  READ_SCODE(length, code, pSymbolName);
77
0
  if (pOS)
78
0
  {
79
0
    (*pOS) << "  " << std::setw(55) << pSymbolName << ": " << code << "\n";
80
0
  }
81
0
}
82
83
void SEIReader::sei_read_code(std::ostream *pOS, uint32_t uiLength, uint32_t& ruiCode, const char *pSymbolName)
84
0
{
85
0
  READ_CODE(uiLength, ruiCode, pSymbolName);
86
0
  if (pOS)
87
0
  {
88
0
    (*pOS) << "  " << std::setw(55) << pSymbolName << ": " << ruiCode << "\n";
89
0
  }
90
0
}
91
92
void SEIReader::sei_read_uvlc(std::ostream *pOS, uint32_t& ruiCode, const char *pSymbolName)
93
0
{
94
0
  READ_UVLC(ruiCode, pSymbolName);
95
0
  if (pOS)
96
0
  {
97
0
    (*pOS) << "  " << std::setw(55) << pSymbolName << ": " << ruiCode << "\n";
98
0
  }
99
0
}
100
101
void SEIReader::sei_read_svlc(std::ostream *pOS, int& ruiCode, const char *pSymbolName)
102
0
{
103
0
  READ_SVLC(ruiCode, pSymbolName);
104
0
  if (pOS)
105
0
  {
106
0
    (*pOS) << "  " << std::setw(55) << pSymbolName << ": " << ruiCode << "\n";
107
0
  }
108
0
}
109
110
void SEIReader::sei_read_flag(std::ostream *pOS, uint32_t& ruiCode, const char *pSymbolName)
111
0
{
112
0
  READ_FLAG(ruiCode, pSymbolName);
113
0
  if (pOS)
114
0
  {
115
0
    (*pOS) << "  " << std::setw(55) << pSymbolName << ": " << (ruiCode?1:0) << "\n";
116
0
  }
117
0
}
118
119
static inline void output_sei_message_header(vvdecSEI* sei, std::ostream *pDecodedMessageOutputStream, uint32_t payloadSize)
120
0
{
121
0
  if (pDecodedMessageOutputStream)
122
0
  {
123
0
    std::string seiMessageHdr(SEI_internal::getSEIMessageString(sei->payloadType)); seiMessageHdr+=" SEI message";
124
0
    (*pDecodedMessageOutputStream) << std::setfill('-') << std::setw((int)seiMessageHdr.size()) << "-" << std::setfill(' ') << "\n" << seiMessageHdr << " (" << payloadSize << " bytes)"<< "\n";
125
0
  }
126
0
}
127
128
#undef READ_CODE
129
#undef READ_SCODE
130
#undef READ_SVLC
131
#undef READ_UVLC
132
#undef READ_FLAG
133
134
135
/**
136
 * unmarshal a single SEI message from bitstream bs
137
 */
138
void SEIReader::parseSEImessage(InputBitstream* bs, seiMessages& seiList,
139
                                const NalUnitType nalUnitType, const uint32_t nuh_layer_id, const uint32_t temporalId,
140
                                const VPS *vps, const SPS *sps, HRD &hrd, std::ostream *pDecodedMessageOutputStream )
141
0
{
142
0
  setBitstream(bs);
143
0
  CHECK(m_pcBitstream->getNumBitsUntilByteAligned(), "Bitstream not aligned");
144
145
0
  seiMessages seiListInCurNalu;
146
0
  do
147
0
  {
148
0
    xReadSEImessage( seiList, nalUnitType, nuh_layer_id, temporalId, vps, sps, hrd, pDecodedMessageOutputStream);
149
0
    if( !seiList.empty() ){  seiListInCurNalu.push_back(seiList.back()); }
150
    /* SEI messages are an integer number of bytes, something has failed
151
    * in the parsing if bitstream not byte-aligned */
152
0
    CHECK(m_pcBitstream->getNumBitsUntilByteAligned(), "Bitstream not aligned");
153
0
  }
154
0
  while (m_pcBitstream->getNumBitsLeft() > 8);
155
156
0
  seiMessages fillerData = SEI_internal::getSeisByType(seiListInCurNalu, VVDEC_FILLER_PAYLOAD);
157
0
  CHECK(fillerData.size() > 0 && fillerData.size() != seiListInCurNalu.size(), "When an SEI NAL unit contains an SEI message with payloadType equal to filler payload, the SEI NAL unit shall not contain any other SEI message with payloadType not equal to filler payload");
158
159
0
  xReadRbspTrailingBits();
160
0
}
161
162
void SEIReader::xReadSEImessage( seiMessages& seiList, const NalUnitType nalUnitType, const uint32_t nuh_layer_id,
163
                                const uint32_t temporalId, const VPS *vps, const SPS *sps, HRD &hrd, std::ostream *pDecodedMessageOutputStream,
164
                                const bool nested)
165
0
{
166
#if ENABLE_TRACING
167
  xTraceSEIHeader();
168
#endif
169
0
  int payloadType = 0;
170
0
  uint32_t val = 0;
171
172
0
  do
173
0
  {
174
0
    sei_read_code(NULL, 8, val, "payload_type");
175
0
    CHECK( payloadType > INT32_MAX - 0xff, "SEI payload_type too large" );
176
0
    payloadType += val;
177
0
  } while (val==0xFF);
178
  // values outside the enum's range (-256..255) are UB to cast
179
0
  const vvdecSEIPayloadType type = payloadType <= 255 ? (vvdecSEIPayloadType) payloadType : VVDEC_SEI_UNKNOWN;
180
181
0
  uint32_t payloadSize = 0;
182
0
  do
183
0
  {
184
0
    sei_read_code(NULL, 8, val, "payload_size");
185
0
    payloadSize += val;
186
0
  } while (val==0xFF);
187
  // also keeps payloadSize * 8 below from wrapping
188
0
  CHECK( payloadSize > getBitstream()->getNumBitsLeft() / 8, "SEI payload_size exceeds the remaining NAL unit size" );
189
190
#if ENABLE_TRACING
191
  xTraceSEIMessageType(type);
192
#endif
193
194
  /* extract the payload for this single SEI message.
195
   * This allows greater safety in erroneous parsing of an SEI message
196
   * from affecting subsequent messages.
197
   * After parsing the payload, bs needs to be restored as the primary
198
   * bitstream.
199
   */
200
0
  InputBitstream *bs = getBitstream();
201
0
  auto substream = bs->extractSubstream( payloadSize * 8 );
202
0
  setBitstream( substream.get() );
203
204
0
  const vvdecSEIBufferingPeriod *bp = NULL;
205
206
0
  vvdecSEI *s = NULL;
207
208
0
  try
209
0
  {
210
0
    if(nalUnitType == NAL_UNIT_PREFIX_SEI)
211
0
    {
212
0
      switch (payloadType)
213
0
      {
214
0
      case VVDEC_USER_DATA_REGISTERED_ITU_T_T35:
215
0
            s = SEI_internal::allocSEI( type ) ;
216
0
            xParseSEIUserDataRegistered( s, payloadSize, pDecodedMessageOutputStream);
217
0
            break;
218
0
      case VVDEC_USER_DATA_UNREGISTERED:
219
0
            s = SEI_internal::allocSEI( type ) ;
220
0
            xParseSEIuserDataUnregistered(s, payloadSize, pDecodedMessageOutputStream);
221
0
        break;
222
0
      case VVDEC_DECODING_UNIT_INFO:
223
0
        bp = hrd.getBufferingPeriodSEI();
224
0
        if (!bp)
225
0
        {
226
0
          msg( WARNING, "Warning: Found Decoding unit information SEI message, but no active buffering period is available. Ignoring.");
227
0
        }
228
0
        else
229
0
        {
230
0
          s = SEI_internal::allocSEI( type ) ;
231
0
          xParseSEIDecodingUnitInfo( s, payloadSize, *bp, temporalId, pDecodedMessageOutputStream);
232
0
        }
233
0
        break;
234
0
      case VVDEC_BUFFERING_PERIOD:
235
0
          s = SEI_internal::allocSEI( type ) ;
236
0
          xParseSEIBufferingPeriod(s, payloadSize, pDecodedMessageOutputStream);
237
0
          if( s )
238
0
          {
239
0
            vvdecSEIBufferingPeriod* bufferingPeriod = reinterpret_cast<vvdecSEIBufferingPeriod *>(s->payload);
240
0
            hrd.setBufferingPeriodSEI(bufferingPeriod);
241
0
          }
242
0
        break;
243
0
      case VVDEC_PICTURE_TIMING:
244
0
        {
245
0
          bp = hrd.getBufferingPeriodSEI();
246
0
          if (!bp)
247
0
          {
248
0
            msg( WARNING, "Warning: Found Picture timing SEI message, but no active buffering period is available. Ignoring.");
249
0
          }
250
0
          else
251
0
          {
252
0
            s = SEI_internal::allocSEI( type ) ;
253
0
            xParseSEIPictureTiming(s, payloadSize, temporalId, *bp, pDecodedMessageOutputStream);
254
0
            if( s )
255
0
            {
256
0
              vvdecSEIPictureTiming* picTiming= reinterpret_cast<vvdecSEIPictureTiming *>(s->payload);
257
0
              hrd.setPictureTimingSEI( picTiming );
258
0
            }
259
0
          }
260
0
        }
261
0
        break;
262
0
      case VVDEC_SCALABLE_NESTING:
263
          // reject here already instead of in xCheckScalableNestingConstraints() to prevent recursion
264
0
          CHECK( nested, "An SEI message that has payloadType equal to scalable nesting shall not be contained in a scalable nesting SEI message" );
265
0
          s = SEI_internal::allocSEI( type ) ;
266
0
          xParseSEIScalableNesting(s, nalUnitType, nuh_layer_id, payloadSize, vps, sps, pDecodedMessageOutputStream);
267
0
        break;
268
0
      case VVDEC_FRAME_FIELD_INFO:
269
0
          s = SEI_internal::allocSEI( type ) ;
270
0
          xParseSEIFrameFieldinfo( s, payloadSize, pDecodedMessageOutputStream);
271
0
        break;
272
0
      case VVDEC_DEPENDENT_RAP_INDICATION:
273
0
          s = SEI_internal::allocSEI( type ) ;
274
0
          xParseSEIDependentRAPIndication( s, payloadSize, pDecodedMessageOutputStream);
275
0
        break;
276
0
      case VVDEC_FRAME_PACKING:
277
0
          s = SEI_internal::allocSEI( type ) ;
278
0
          xParseSEIFramePacking( s, payloadSize, pDecodedMessageOutputStream);
279
0
        break;
280
0
      case VVDEC_PARAMETER_SETS_INCLUSION_INDICATION:
281
0
          s = SEI_internal::allocSEI( type ) ;
282
0
          xParseSEIParameterSetsInclusionIndication( s, payloadSize, pDecodedMessageOutputStream);
283
0
        break;
284
0
      case VVDEC_MASTERING_DISPLAY_COLOUR_VOLUME:
285
0
          s = SEI_internal::allocSEI( type ) ;
286
0
          xParseSEIMasteringDisplayColourVolume( s, payloadSize, pDecodedMessageOutputStream);
287
0
        break;
288
0
  #if U0033_ALTERNATIVE_TRANSFER_CHARACTERISTICS_SEI
289
0
      case VVDEC_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
290
0
          s = SEI_internal::allocSEI( type ) ;
291
0
          xParseSEIAlternativeTransferCharacteristics( s, payloadSize, pDecodedMessageOutputStream);
292
0
        break;
293
0
  #endif
294
0
      case VVDEC_EQUIRECTANGULAR_PROJECTION:
295
0
          s = SEI_internal::allocSEI( type ) ;
296
0
          xParseSEIEquirectangularProjection( s, payloadSize, pDecodedMessageOutputStream);
297
0
        break;
298
0
      case VVDEC_SPHERE_ROTATION:
299
0
          s = SEI_internal::allocSEI( type ) ;
300
0
          xParseSEISphereRotation( s, payloadSize, pDecodedMessageOutputStream);
301
0
        break;
302
0
      case VVDEC_OMNI_VIEWPORT:
303
0
          s = SEI_internal::allocSEI( type ) ;
304
0
          xParseSEIOmniViewport( s, payloadSize, pDecodedMessageOutputStream);
305
0
        break;
306
0
      case VVDEC_REGION_WISE_PACKING:
307
0
          s = SEI_internal::allocSEI( type ) ;
308
0
          xParseSEIRegionWisePacking( s, payloadSize, pDecodedMessageOutputStream);
309
0
        break;
310
0
      case VVDEC_GENERALIZED_CUBEMAP_PROJECTION:
311
0
          s = SEI_internal::allocSEI( type ) ;
312
0
          xParseSEIGeneralizedCubemapProjection( s, payloadSize, pDecodedMessageOutputStream);
313
0
        break;
314
0
      case VVDEC_SUBPICTURE_LEVEL_INFO:
315
0
          s = SEI_internal::allocSEI( type ) ;
316
0
          xParseSEISubpictureLevelInfo(s, payloadSize, pDecodedMessageOutputStream);
317
0
        break;
318
0
      case VVDEC_SAMPLE_ASPECT_RATIO_INFO:
319
0
          s = SEI_internal::allocSEI( type ) ;
320
0
          xParseSEISampleAspectRatioInfo( s, payloadSize, pDecodedMessageOutputStream);
321
0
        break;
322
0
      case VVDEC_FILM_GRAIN_CHARACTERISTICS:
323
0
          s = SEI_internal::allocSEI( type ) ;
324
0
          xParseSEIFilmGrainCharacteristics(s, payloadSize, pDecodedMessageOutputStream);
325
0
        break;
326
0
      case VVDEC_CONTENT_LIGHT_LEVEL_INFO:
327
0
          s = SEI_internal::allocSEI( type ) ;
328
0
          xParseSEIContentLightLevelInfo( s, payloadSize, pDecodedMessageOutputStream);
329
0
        break;
330
0
      case VVDEC_AMBIENT_VIEWING_ENVIRONMENT:
331
0
          s = SEI_internal::allocSEI( type ) ;
332
0
          xParseSEIAmbientViewingEnvironment( s, payloadSize, pDecodedMessageOutputStream);
333
0
        break;
334
0
      case VVDEC_CONTENT_COLOUR_VOLUME:
335
0
          s = SEI_internal::allocSEI( type ) ;
336
0
          xParseSEIContentColourVolume( s, payloadSize, pDecodedMessageOutputStream);
337
0
        break;
338
0
      default:
339
0
        for (uint32_t i = 0; i < payloadSize; i++)
340
0
        {
341
0
          uint32_t seiByte;
342
0
          sei_read_code (NULL, 8, seiByte, "unknown prefix SEI payload byte");
343
0
        }
344
0
        msg( WARNING, "Unknown prefix SEI message (payloadType = %d) was found!\n", payloadType);
345
0
        if (pDecodedMessageOutputStream)
346
0
        {
347
0
          (*pDecodedMessageOutputStream) << "Unknown prefix SEI message (payloadType = " << payloadType << ") was found!\n";
348
0
        }
349
0
        break;
350
0
      }
351
0
    }
352
0
    else
353
0
    {
354
0
      switch (payloadType)
355
0
      {
356
0
        case VVDEC_USER_DATA_UNREGISTERED:
357
0
          s = SEI_internal::allocSEI( type ) ;
358
0
          xParseSEIuserDataUnregistered(s, payloadSize, pDecodedMessageOutputStream);
359
0
          break;
360
0
        case  VVDEC_DECODED_PICTURE_HASH:
361
0
          s = SEI_internal::allocSEI( type ) ;
362
0
          xParseSEIDecodedPictureHash(s, payloadSize, pDecodedMessageOutputStream);
363
0
          break;
364
0
        case VVDEC_SCALABLE_NESTING:
365
          // reject here already instead of in xCheckScalableNestingConstraints() to prevent recursion
366
0
          CHECK( nested, "An SEI message that has payloadType equal to scalable nesting shall not be contained in a scalable nesting SEI message" );
367
0
          s = SEI_internal::allocSEI( type ) ;
368
0
          xParseSEIScalableNesting(s, nalUnitType, nuh_layer_id, payloadSize, vps, sps, pDecodedMessageOutputStream);
369
0
          break;
370
0
        default:
371
0
          for (uint32_t i = 0; i < payloadSize; i++)
372
0
          {
373
0
            uint32_t seiByte;
374
0
            sei_read_code( NULL, 8, seiByte, "unknown suffix SEI payload byte");
375
0
          }
376
0
          msg( WARNING, "Unknown suffix SEI message (payloadType = %d) was found!\n", payloadType);
377
0
          if (pDecodedMessageOutputStream)
378
0
          {
379
0
            (*pDecodedMessageOutputStream) << "Unknown suffix SEI message (payloadType = " << payloadType << ") was found!\n";
380
0
          }
381
0
          break;
382
0
      }
383
0
    }
384
385
0
    if( s )
386
0
    {
387
0
      seiList.push_back(s);
388
0
    }
389
0
  }
390
0
  catch( ... )
391
0
  {
392
0
    SEI_internal::deleteSEI( s );
393
0
    throw;
394
0
  }
395
396
  /* By definition the underlying bitstream terminates in a byte-aligned manner.
397
   * 1. Extract all bar the last MIN(bitsremaining,nine) bits as reserved_payload_extension_data
398
   * 2. Examine the final 8 bits to determine the payload_bit_equal_to_one marker
399
   * 3. Extract the remainingreserved_payload_extension_data bits.
400
   *
401
   * If there are fewer than 9 bits available, extract them.
402
   */
403
0
  int payloadBitsRemaining = getBitstream()->getNumBitsLeft();
404
0
  if (payloadBitsRemaining) /* more_data_in_payload() */
405
0
  {
406
0
    for (; payloadBitsRemaining > 9; payloadBitsRemaining--)
407
0
    {
408
0
      uint32_t reservedPayloadExtensionData;
409
0
      sei_read_code ( pDecodedMessageOutputStream, 1, reservedPayloadExtensionData, "reserved_payload_extension_data");
410
0
    }
411
412
    /* 2 */
413
0
    int finalBits = getBitstream()->peekBits(payloadBitsRemaining);
414
0
    int finalPayloadBits = 0;
415
0
    for (int mask = 0xff; finalBits & (mask >> finalPayloadBits); finalPayloadBits++)
416
0
    {
417
0
      continue;
418
0
    }
419
420
    /* 3 */
421
0
    for (; payloadBitsRemaining > 9 - finalPayloadBits; payloadBitsRemaining--)
422
0
    {
423
0
      uint32_t reservedPayloadExtensionData;
424
0
      sei_read_flag ( 0, reservedPayloadExtensionData, "reserved_payload_extension_data");
425
0
    }
426
427
0
    uint32_t dummy;
428
0
    sei_read_flag( 0, dummy, "payload_bit_equal_to_one"); payloadBitsRemaining--;
429
0
    while (payloadBitsRemaining)
430
0
    {
431
0
      sei_read_flag( 0, dummy, "payload_bit_equal_to_zero"); payloadBitsRemaining--;
432
0
    }
433
0
  }
434
435
  /* restore primary bitstream for sei_message */
436
0
  setBitstream( bs );
437
0
}
438
439
440
441
/**
442
 * parse bitstream bs and unpack a decoded picture hash SEI message
443
 * of payloadSize bytes into sei.
444
 */
445
void SEIReader::xParseSEIDecodedPictureHash(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
446
0
{
447
0
  uint32_t bytesRead = 0;
448
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
449
450
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIDecodedPictureHash" );
451
452
0
  vvdecSEIDecodedPictureHash* sei =(vvdecSEIDecodedPictureHash*)s->payload;
453
0
  ::memset(sei, 0, sizeof(vvdecSEIDecodedPictureHash));
454
455
0
  uint32_t val;
456
0
  sei_read_code( pDecodedMessageOutputStream, 8, val, "dph_sei_hash_type" );
457
0
  CHECK_READ_RANGE( val, 0, VVDEC_HASHTYPE_NONE, "dph_sei_hash_type" );
458
0
  sei->method = static_cast<vvdecHashType>(val); bytesRead++;
459
0
  sei_read_code( pDecodedMessageOutputStream, 1, val, "dph_sei_single_component_flag");
460
0
  sei->singleCompFlag = val;
461
0
  sei_read_code( pDecodedMessageOutputStream, 7, val, "dph_sei_reserved_zero_7bits");
462
0
  bytesRead++;
463
0
  uint32_t expectedSize = ( sei->singleCompFlag ? 1 : 3 ) * (sei->method == 0 ? 16 : (sei->method == 1 ? 2 : 4));
464
0
  CHECK ((payloadSize - bytesRead) != expectedSize, "The size of the decoded picture hash does not match the expected size.");
465
466
0
  const char *traceString="\0";
467
0
  switch (sei->method)
468
0
  {
469
0
    case VVDEC_HASHTYPE_MD5: traceString="picture_md5"; break;
470
0
    case VVDEC_HASHTYPE_CRC: traceString="picture_crc"; break;
471
0
    case VVDEC_HASHTYPE_CHECKSUM: traceString="picture_checksum"; break;
472
0
    default: THROW_RECOVERABLE("Unknown hash type"); break;
473
0
  }
474
475
0
  if (pDecodedMessageOutputStream)
476
0
  {
477
0
    (*pDecodedMessageOutputStream) << "  " << std::setw(55) << traceString << ": " << std::hex << std::setfill('0');
478
0
  }
479
480
0
  CHECK( payloadSize-bytesRead > 48, "payload size of digest must be <= 48 in vvdecSEIDecodedPictureHash" );
481
0
  for(int i=0;bytesRead < payloadSize; bytesRead++,i++)
482
0
  {
483
0
    sei_read_code( NULL, 8, val, traceString);
484
0
    sei->digest[i] = (uint8_t)val;
485
0
    sei->digest_length++;
486
487
0
    if (pDecodedMessageOutputStream)
488
0
    {
489
0
      (*pDecodedMessageOutputStream) << std::setw(2) << val;
490
0
    }
491
0
  }
492
493
0
  if (pDecodedMessageOutputStream)
494
0
  {
495
0
    (*pDecodedMessageOutputStream) << std::dec << std::setfill(' ') << "\n";
496
0
  }
497
0
}
498
499
void SEIReader::xParseSEIScalableNesting(vvdecSEI* s, const NalUnitType nalUnitType, const uint32_t nuhLayerId, uint32_t payloadSize, const VPS *vps, const SPS *sps, std::ostream *decodedMessageOutputStream)
500
0
{
501
0
  uint32_t symbol;
502
0
  output_sei_message_header(s, decodedMessageOutputStream, payloadSize);
503
504
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIScalableNesting" );
505
0
  vvdecSEIScalableNesting* sei =(vvdecSEIScalableNesting*)s->payload;
506
0
  ::memset(sei, 0, sizeof(vvdecSEIScalableNesting));
507
508
0
  sei_read_flag(decodedMessageOutputStream, symbol, "sn_ols_flag");    sei->snOlsFlag = symbol;
509
0
  sei_read_flag(decodedMessageOutputStream, symbol, "sn_subpic_flag"); sei->snSubpicFlag = symbol;
510
0
  if (sei->snOlsFlag)
511
0
  {
512
0
    sei_read_uvlc(decodedMessageOutputStream, symbol, "sn_num_olss_minus1");
513
0
    CHECK( symbol >= 64, "sn_num_olss_minus1 must be < 64 in vvdecSEIScalableNesting" );
514
0
    sei->snNumOlss = symbol+1;
515
516
0
    for (uint32_t i = 0; i < sei->snNumOlss; i++)
517
0
    {
518
0
      sei_read_uvlc(decodedMessageOutputStream, symbol, "sn_ols_idx_delta_minus1[i]"); sei->snOlsIdxDelta[i] = symbol+1;
519
0
      CHECK( vps && vps->getVPSId() != 0 && sei->snOlsIdxDelta[i] >= (uint32_t) vps->getTotalNumOLSs(),"sn_ols_idx_delta_minus1 must be <= TotalNumOlss - 2 in vvdecSEIScalableNesting" );
520
0
    }
521
0
    for (uint32_t i = 0; i < sei->snNumOlss; i++)
522
0
    {
523
0
      if (i == 0)
524
0
      {
525
0
        sei->snOlsIdx[i] = sei->snOlsIdxDelta[i]-1;
526
0
      }
527
0
      else
528
0
      {
529
        // NestingOlsIdx[i] = NestingOlsIdx[i - 1] + sn_ols_idx_delta_minus1[i] + 1, and snOlsIdxDelta holds the delta itself
530
0
        sei->snOlsIdx[i] = sei->snOlsIdx[i - 1] + sei->snOlsIdxDelta[i];
531
0
      }
532
0
    }
533
0
    if (vps && vps->getVPSId() != 0)
534
0
    {
535
0
      uint32_t lowestLayerId = MAX_UINT;
536
0
      for (uint32_t olsIdxForSEI = 0; olsIdxForSEI < sei->snNumOlss; olsIdxForSEI++)
537
0
      {
538
0
        CHECK( sei->snOlsIdx[olsIdxForSEI] >= (uint32_t) vps->getTotalNumOLSs(), "NestingOlsIdx must be < TotalNumOlss in vvdecSEIScalableNesting" );
539
0
        int olsIdx = sei->snOlsIdx[olsIdxForSEI];
540
0
        for (int layerIdx = 0; layerIdx < vps->getNumLayersInOls(olsIdx); layerIdx++)
541
0
        {
542
0
          if (lowestLayerId > vps->getLayerIdInOls(olsIdx, layerIdx))
543
0
          {
544
0
            lowestLayerId = vps->getLayerIdInOls(olsIdx, layerIdx);
545
0
          }
546
0
        }
547
0
      }
548
0
      CHECK(lowestLayerId!= nuhLayerId, "nuh_layer_id is not equal to the lowest layer among Olss that the scalable SEI applies");
549
0
    }
550
0
  }
551
0
  else
552
0
  {
553
0
    sei_read_flag(decodedMessageOutputStream, symbol, "sn_all_layers_flag"); sei->snAllLayersFlag = symbol;
554
0
    if (!sei->snAllLayersFlag)
555
0
    {
556
0
      sei_read_uvlc(decodedMessageOutputStream, symbol, "sn_num_layers_minus1");
557
0
      CHECK( symbol >= 64, "sn_num_layers_minus1 must be < 64 in vvdecSEIScalableNesting" );
558
0
      sei->snNumLayers = symbol+1;
559
0
      sei->snLayerId[0] = nuhLayerId;
560
0
      for (uint32_t i = 1; i < sei->snNumLayers; i++)
561
0
      {
562
0
        sei_read_code(decodedMessageOutputStream, 6, symbol, "sn_layer_id[i]"); sei->snLayerId[i] = symbol;
563
0
      }
564
0
    }
565
0
  }
566
0
  if (sei->snSubpicFlag)
567
0
  {
568
0
    sei_read_uvlc(decodedMessageOutputStream, symbol, "sn_num_subpics_minus1");
569
0
    CHECK( symbol >= 64, "sn_num_subpics_minus1 must be < 64 in vvdecSEIScalableNesting" );
570
0
    sei->snNumSubpics = symbol + 1;
571
0
    sei_read_uvlc(decodedMessageOutputStream, symbol, "sn_subpic_id_len_minus1");
572
0
    CHECK( symbol >= 16, "sn_subpic_id_len_minus1 must be < 16 in vvdecSEIScalableNesting" );
573
0
    sei->snSubpicIdLen = symbol + 1;
574
0
    for (uint32_t i = 0; i < sei->snNumSubpics; i++)
575
0
    {
576
0
      sei_read_code(decodedMessageOutputStream, sei->snSubpicIdLen, symbol, "sn_subpic_id[i]"); sei->snSubpicId[i] = symbol;
577
0
    }
578
0
  }
579
580
0
  sei_read_uvlc(decodedMessageOutputStream, symbol, "sn_num_seis_minus1");
581
0
  CHECK( symbol >= 64, "The value of sn_num_seis_minus1 shall be in the range of 0 to 63");
582
0
  sei->snNumSEIs = symbol + 1;
583
584
  // byte alignment
585
0
  while( m_pcBitstream->getNumBitsUntilByteAligned() )
586
0
  {
587
0
    sei_read_flag(decodedMessageOutputStream, symbol, "sn_zero_bit");
588
0
  }
589
590
  // read nested SEI messages
591
0
  for (uint32_t i=0; i<sei->snNumSEIs; i++)
592
0
  {
593
0
    seiMessages tmpSeiList;
594
0
    xReadSEImessage(tmpSeiList, nalUnitType, nuhLayerId, 0, vps, sps, m_nestedHrd, decodedMessageOutputStream, true);
595
0
    CHECK( tmpSeiList.empty(), "read empty nested sei list." );
596
597
0
    sei->nestedSEIs[i] = tmpSeiList.front();
598
0
  }
599
600
0
  xCheckScalableNestingConstraints(sei, nalUnitType, vps);
601
602
0
  if (decodedMessageOutputStream)
603
0
  {
604
0
    (*decodedMessageOutputStream) << "End of scalable nesting SEI message\n";
605
0
  }
606
0
}
607
608
void SEIReader::xCheckScalableNestingConstraints(const vvdecSEIScalableNesting* sei, const NalUnitType nalUnitType, const VPS* vps)
609
0
{
610
0
  const std::vector<int> vclAssociatedSeiList { 3, 19, 45, 129, 137, 144, 145, 147, 148, 149, 150, 153, 154, 155, 156, 168, 204 };
611
612
0
  bool containBPorPTorDUIorSLI = false;
613
0
  bool containNoBPorPTorDUIorSLI = false;
614
615
0
  for (auto* nestedsei : sei->nestedSEIs)
616
0
  {
617
0
    if( !nestedsei )
618
0
    {
619
0
      continue;
620
0
    }
621
0
    CHECK(nestedsei->payloadType == VVDEC_FILLER_PAYLOAD || nestedsei->payloadType == VVDEC_SCALABLE_NESTING, "An SEI message that has payloadType equal to filler payload or scalable nesting shall not be contained in a scalable nesting SEI message");
622
623
0
    CHECK(nestedsei->payloadType != VVDEC_FILLER_PAYLOAD && nestedsei->payloadType != VVDEC_DECODED_PICTURE_HASH && nalUnitType != NAL_UNIT_PREFIX_SEI, "When a scalable nesting SEI message contains an SEI message that has payloadType not equal to filler payload or decoded picture hash, the SEI NAL unit containing the scalable nesting SEI message shall have nal_unit_type equal to PREFIX_SEI_NUT");
624
625
0
    CHECK(nestedsei->payloadType == VVDEC_DECODED_PICTURE_HASH && nalUnitType != NAL_UNIT_SUFFIX_SEI, "When a scalable nesting SEI message contains an SEI message that has payloadType equal to decoded picture hash, the SEI NAL unit containing the scalable nesting SEI message shall have nal_unit_type equal to SUFFIX_SEI_NUT");
626
627
0
    CHECK(nestedsei->payloadType == VVDEC_DECODED_PICTURE_HASH && !sei->snSubpicFlag, "When the scalable nesting SEI message contains an SEI message that has payloadType equal to decoded picture hash, the value of sn_subpic_flag shall be equal to 1");
628
629
0
    CHECK(nestedsei->payloadType == VVDEC_SUBPICTURE_LEVEL_INFO && sei->snSubpicFlag, "When the scalable nesting SEI message contains an SEI message that has payloadType equal to SLI, the value of sn_subpic_flag shall be equal to 0");
630
631
0
    if( vps )
632
0
    {
633
0
      CHECK( vps->getGeneralHrdParameters()->getGeneralSamePicTimingInAllOlsFlag() && nestedsei->payloadType == VVDEC_PICTURE_TIMING,
634
0
             "When general_same_pic_timing_in_all_ols_flag is equal to 1, there shall be no SEI NAL unit that contain a scalable-nested SEI message with payloadType equal to PT" );
635
0
    }
636
637
0
    for (int i = 0; i < (int)vclAssociatedSeiList.size(); i++)
638
0
    {
639
0
      CHECK(nestedsei->payloadType == vclAssociatedSeiList[i] && sei->snOlsFlag, "When the scalable nesting SEI message contains an SEI message that has payloadType equal to a value in vclAssociatedSeiList, the value of sn_ols_flag shall be equal to 0");
640
0
    }
641
642
0
    if (nestedsei->payloadType == VVDEC_BUFFERING_PERIOD || nestedsei->payloadType == VVDEC_PICTURE_TIMING || nestedsei->payloadType == VVDEC_DECODING_UNIT_INFO || nestedsei->payloadType == VVDEC_SUBPICTURE_LEVEL_INFO)
643
0
    {
644
0
      containBPorPTorDUIorSLI = true;
645
0
      CHECK(!sei->snOlsFlag, "When the scalable nesting SEI message contains an SEI message that has payloadType equal to BP, PT, or DUI, or SLI, the value of sn_ols_flag shall be equal to 1");
646
0
    }
647
0
    if (!(nestedsei->payloadType == VVDEC_BUFFERING_PERIOD || nestedsei->payloadType == VVDEC_PICTURE_TIMING || nestedsei->payloadType == VVDEC_DECODING_UNIT_INFO || nestedsei->payloadType == VVDEC_SUBPICTURE_LEVEL_INFO))
648
0
    {
649
0
      containNoBPorPTorDUIorSLI = true;
650
0
    }
651
0
  }
652
0
  CHECK(containBPorPTorDUIorSLI && containNoBPorPTorDUIorSLI, "When a scalable nesting SEI message contains a BP, PT, DUI, or SLI SEI message, the scalable nesting SEI message shall not contain any other SEI message with payloadType not equal to BP, PT, DUI, or SLI");
653
0
}
654
655
void SEIReader::xParseSEIDecodingUnitInfo(vvdecSEI* s, uint32_t payloadSize, const vvdecSEIBufferingPeriod& bp, const uint32_t temporalId, std::ostream *pDecodedMessageOutputStream)
656
0
{
657
0
  uint32_t val;
658
659
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
660
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIDecodingUnitInfo" );
661
0
  vvdecSEIDecodingUnitInfo* sei =(vvdecSEIDecodingUnitInfo*)s->payload;
662
0
  ::memset(sei, 0, sizeof(vvdecSEIDecodingUnitInfo));
663
664
0
  sei_read_uvlc( pDecodedMessageOutputStream, val, "decoding_unit_idx");
665
0
  sei->decodingUnitIdx = val;
666
667
0
  if(!bp.decodingUnitCpbParamsInPicTimingSeiFlag)
668
0
  {
669
0
    for (int i = temporalId; i <= bp.bpMaxSubLayers - 1; i++)
670
0
    {
671
0
      if (i < (bp.bpMaxSubLayers - 1))
672
0
      {
673
0
        sei_read_flag( pDecodedMessageOutputStream, val, "dui_sub_layer_delays_present_flag[i]" );
674
0
        sei->duiSubLayerDelaysPresentFlag[i] = val;
675
0
      }
676
0
      else
677
0
      {
678
0
        sei->duiSubLayerDelaysPresentFlag[i] = 1;
679
0
      }
680
0
      if( sei->duiSubLayerDelaysPresentFlag[i] )
681
0
      {
682
0
        sei_read_code( pDecodedMessageOutputStream, bp.duCpbRemovalDelayIncrementLength, val, "du_spt_cpb_removal_delay_increment[i]");
683
0
        sei->duSptCpbRemovalDelayIncrement[i] = val;
684
0
      }
685
0
      else
686
0
      {
687
0
        sei->duSptCpbRemovalDelayIncrement[i] = 0;
688
0
      }
689
0
    }
690
0
  }
691
0
  else
692
0
  {
693
0
    for( int i = temporalId; i < bp.bpMaxSubLayers - 1; i ++ )
694
0
    {
695
0
      sei->duSptCpbRemovalDelayIncrement[i] = 0;
696
0
    }
697
0
  }
698
0
  if (!bp.decodingUnitDpbDuParamsInPicTimingSeiFlag)
699
0
  {
700
0
    sei_read_flag( pDecodedMessageOutputStream, val, "dpb_output_du_delay_present_flag"); sei->dpbOutputDuDelayPresentFlag = (val != 0);
701
0
  }
702
0
  else
703
0
  {
704
0
    sei->dpbOutputDuDelayPresentFlag = false;
705
0
  }
706
0
  if(sei->dpbOutputDuDelayPresentFlag)
707
0
  {
708
0
    sei_read_code( pDecodedMessageOutputStream, bp.dpbOutputDelayDuLength, val, "pic_spt_dpb_output_du_delay");
709
0
    sei->picSptDpbOutputDuDelay = val;
710
0
  }
711
0
}
712
713
void SEIReader::xParseSEIBufferingPeriod(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
714
0
{
715
0
  int i, nalOrVcl;
716
0
  uint32_t code;
717
718
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
719
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIBufferingPeriod" );
720
0
  vvdecSEIBufferingPeriod* sei =(vvdecSEIBufferingPeriod*)s->payload;
721
0
  ::memset(sei, 0, sizeof(vvdecSEIBufferingPeriod));
722
723
0
  sei_read_flag( pDecodedMessageOutputStream, code, "bp_nal_hrd_parameters_present_flag" );             sei->bpNalCpbParamsPresentFlag = code;
724
0
  sei_read_flag( pDecodedMessageOutputStream, code, "bp_vcl_hrd_parameters_present_flag" );             sei->bpVclCpbParamsPresentFlag = code;
725
726
0
  sei_read_code( pDecodedMessageOutputStream, 5, code, "initial_cpb_removal_delay_length_minus1" );     sei->initialCpbRemovalDelayLength = code + 1;
727
0
  sei_read_code( pDecodedMessageOutputStream, 5, code, "cpb_removal_delay_length_minus1" );             sei->cpbRemovalDelayLength        = code + 1;
728
0
  sei_read_code( pDecodedMessageOutputStream, 5, code, "dpb_output_delay_length_minus1" );              sei->dpbOutputDelayLength         = code + 1;
729
0
  sei_read_flag( pDecodedMessageOutputStream, code, "bp_decoding_unit_hrd_params_present_flag" );       sei->bpDecodingUnitHrdParamsPresentFlag = code;
730
0
  if( sei->bpDecodingUnitHrdParamsPresentFlag )
731
0
  {
732
0
    sei_read_code( pDecodedMessageOutputStream, 5, code, "du_cpb_removal_delay_increment_length_minus1" );  sei->duCpbRemovalDelayIncrementLength = code + 1;
733
0
    sei_read_code( pDecodedMessageOutputStream, 5, code, "dpb_output_delay_du_length_minus1" );             sei->dpbOutputDelayDuLength = code + 1;
734
0
    sei_read_flag( pDecodedMessageOutputStream, code, "decoding_unit_cpb_params_in_pic_timing_sei_flag" );  sei->decodingUnitCpbParamsInPicTimingSeiFlag = code;
735
0
    sei_read_flag(pDecodedMessageOutputStream, code, "decoding_unit_dpb_du_params_in_pic_timing_sei_flag"); sei->decodingUnitDpbDuParamsInPicTimingSeiFlag = code;
736
0
  }
737
0
  else
738
0
  {
739
0
    sei->duCpbRemovalDelayIncrementLength = 24;
740
0
    sei->dpbOutputDelayDuLength = 24;
741
0
    sei->decodingUnitDpbDuParamsInPicTimingSeiFlag = false;
742
0
  }
743
744
0
  sei_read_flag( pDecodedMessageOutputStream, code, "concatenation_flag");
745
0
  sei->concatenationFlag = code;
746
0
  sei_read_flag ( pDecodedMessageOutputStream, code, "additional_concatenation_info_present_flag");
747
0
  sei->additionalConcatenationInfoPresentFlag = code;
748
0
  if (sei->additionalConcatenationInfoPresentFlag)
749
0
  {
750
0
    sei_read_code( pDecodedMessageOutputStream, sei->initialCpbRemovalDelayLength, code, "max_initial_removal_delay_for_concatenation" );
751
0
    sei->maxInitialRemovalDelayForConcatenation = code;
752
0
  }
753
754
0
  sei_read_code( pDecodedMessageOutputStream, ( sei->cpbRemovalDelayLength ), code, "au_cpb_removal_delay_delta_minus1" );
755
0
  sei->auCpbRemovalDelayDelta = code + 1;
756
757
0
  sei_read_code(pDecodedMessageOutputStream, 3, code, "bp_max_sub_layers_minus1");
758
0
  sei->bpMaxSubLayers = code + 1;
759
0
  CHECK( sei->bpMaxSubLayers > 7, "bp_max_sub_layers_minus1 must be <= 6 in vvdecSEIBufferingPeriod" );
760
0
  if (sei->bpMaxSubLayers - 1 > 0)
761
0
  {
762
0
    sei_read_flag(pDecodedMessageOutputStream, code, "cpb_removal_delay_deltas_present_flag");
763
0
    sei->cpbRemovalDelayDeltasPresentFlag = code;
764
0
  }
765
0
  else
766
0
  {
767
0
    sei->cpbRemovalDelayDeltasPresentFlag = false;
768
0
  }
769
0
  if (sei->cpbRemovalDelayDeltasPresentFlag)
770
0
  {
771
0
    sei_read_uvlc( pDecodedMessageOutputStream, code, "num_cpb_removal_delay_deltas_minus1" );             sei->numCpbRemovalDelayDeltas = code + 1;
772
0
    CHECK( sei->numCpbRemovalDelayDeltas < 0 || sei->numCpbRemovalDelayDeltas > 14, "num_cpb_removal_delay_deltas_minus1 must be <= 13 in vvdecSEIBufferingPeriod" );
773
774
0
    for( i = 0; i < sei->numCpbRemovalDelayDeltas; i ++ )
775
0
    {
776
0
      sei_read_code( pDecodedMessageOutputStream, ( sei->cpbRemovalDelayLength ), code, "cpb_removal_delay_delta[i]" );
777
0
      sei->cpbRemovalDelayDelta[ i ] = code;
778
0
    }
779
0
  }
780
0
  sei_read_uvlc( pDecodedMessageOutputStream, code, "bp_cpb_cnt_minus1" ); sei->bpCpbCnt = code + 1;
781
0
  CHECK( sei->bpCpbCnt < 0 || sei->bpCpbCnt > 32, "bp_cpb_cnt_minus1 must be <= 31 in vvdecSEIBufferingPeriod" );
782
0
  if (sei->bpMaxSubLayers - 1 > 0)
783
0
  {
784
0
    sei_read_flag(pDecodedMessageOutputStream, code, "bp_sublayer_initial_cpb_removal_delay_present_flag");
785
0
    sei->sublayerInitialCpbRemovalDelayPresentFlag = code;
786
0
  }
787
0
  else
788
0
  {
789
0
    sei->sublayerInitialCpbRemovalDelayPresentFlag = false;
790
0
  }
791
0
  for (i = (sei->sublayerInitialCpbRemovalDelayPresentFlag ? 0 : sei->bpMaxSubLayers - 1); i < sei->bpMaxSubLayers; i++)
792
0
  {
793
0
    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
794
0
    {
795
0
      if( ( ( nalOrVcl == 0 ) && ( sei->bpNalCpbParamsPresentFlag ) ) ||
796
0
         ( ( nalOrVcl == 1 ) && ( sei->bpVclCpbParamsPresentFlag ) ) )
797
0
      {
798
0
        for( int j = 0; j < ( sei->bpCpbCnt ); j ++ )
799
0
        {
800
0
          sei_read_code( pDecodedMessageOutputStream, sei->initialCpbRemovalDelayLength, code, nalOrVcl ? "vcl_initial_cpb_removal_delay[i][j]" : "nal_initial_cpb_removal_delay[i][j]" );
801
0
          sei->initialCpbRemovalDelay[i][j][nalOrVcl] = code;
802
0
          sei_read_code( pDecodedMessageOutputStream, sei->initialCpbRemovalDelayLength, code, nalOrVcl ? "vcl_initial_cpb_removal_offset[i][j]" : "nal_initial_cpb_removal_offset[i][j]" );
803
0
          sei->initialCpbRemovalOffset[i][j][nalOrVcl] = code;
804
0
          if( sei->bpDecodingUnitHrdParamsPresentFlag )
805
0
          {
806
            // alternative initial CPB removal delay/offset are not stored
807
0
            sei_read_code( pDecodedMessageOutputStream, sei->initialCpbRemovalDelayLength, code, nalOrVcl ? "vcl_initial_alt_cpb_removal_delay[i][j]" : "nal_initial_alt_cpb_removal_delay[i][j]" );
808
0
            sei_read_code( pDecodedMessageOutputStream, sei->initialCpbRemovalDelayLength, code, nalOrVcl ? "vcl_initial_alt_cpb_removal_offset[i][j]" : "nal_initial_alt_cpb_removal_offset[i][j]" );
809
0
          }
810
0
        }
811
0
      }
812
0
    }
813
0
  }
814
0
  if (sei->bpMaxSubLayers-1 > 0)
815
0
  {
816
0
    sei_read_flag(pDecodedMessageOutputStream, code, "bp_sublayer_dpb_output_offsets_present_flag");
817
0
    sei->sublayerDpbOutputOffsetsPresentFlag = code;
818
0
  }
819
0
  else
820
0
  {
821
0
    sei->sublayerDpbOutputOffsetsPresentFlag = false;
822
0
  }
823
0
  if(sei->sublayerDpbOutputOffsetsPresentFlag)
824
0
  {
825
0
    for(int i = 0; i < sei->bpMaxSubLayers - 1; i++)
826
0
    {
827
0
      sei_read_uvlc( pDecodedMessageOutputStream, code, "dpb_output_tid_offset[i]" );
828
0
      sei->dpbOutputTidOffset[i] = code;
829
0
    }
830
0
    sei->dpbOutputTidOffset[sei->bpMaxSubLayers-1] = 0;
831
0
  }
832
0
  sei_read_flag(pDecodedMessageOutputStream, code, "bp_alt_cpb_params_present_flag");
833
0
  sei->altCpbParamsPresentFlag = code;
834
0
  if (sei->altCpbParamsPresentFlag)
835
0
  {
836
0
    sei_read_flag(pDecodedMessageOutputStream, code, "use_alt_cpb_params_flag"); sei->useAltCpbParamsFlag = code;
837
0
  }
838
0
  CHECK(sei->altCpbParamsPresentFlag && sei->bpDecodingUnitHrdParamsPresentFlag,"When bp_alt_cpb_params_present_flag is equal to 1, the value of bp_du_hrd_params_present_flag shall be equal to 0");
839
840
0
}
841
842
void SEIReader::xParseSEIPictureTiming(vvdecSEI* s, uint32_t payloadSize, const uint32_t temporalId, const vvdecSEIBufferingPeriod& bp, std::ostream *pDecodedMessageOutputStream)
843
0
{
844
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIPictureTiming" );
845
846
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
847
848
0
  vvdecSEIPictureTiming* sei =(vvdecSEIPictureTiming*)s->payload;
849
0
  ::memset(sei, 0, sizeof(vvdecSEIPictureTiming));
850
851
0
  uint32_t symbol;
852
0
  sei_read_code( pDecodedMessageOutputStream, bp.cpbRemovalDelayLength, symbol, "pt_cpb_removal_delay_minus1[bp_max_sub_layers_minus1]" );
853
0
  sei->auCpbRemovalDelay[bp.bpMaxSubLayers - 1] = symbol + 1;
854
0
  sei->ptSubLayerDelaysPresentFlag[bp.bpMaxSubLayers - 1] = true;
855
0
  for (int i = temporalId; i < bp.bpMaxSubLayers - 1; i++)
856
0
  {
857
0
    sei_read_flag(pDecodedMessageOutputStream, symbol, "pt_sub_layer_delays_present_flag[i]");
858
0
    sei->ptSubLayerDelaysPresentFlag[i] = (symbol == 1);
859
0
    if (sei->ptSubLayerDelaysPresentFlag[i])
860
0
    {
861
0
      if (bp.cpbRemovalDelayDeltasPresentFlag)
862
0
      {
863
0
        sei_read_flag(pDecodedMessageOutputStream, symbol, "pt_cpb_removal_delay_delta_enabled_flag[i]");
864
0
        sei->cpbRemovalDelayDeltaEnabledFlag[i] = (symbol == 1);
865
0
      }
866
0
      else
867
0
      {
868
0
        sei->cpbRemovalDelayDeltaEnabledFlag[i] = false;
869
0
      }
870
0
      if (sei->cpbRemovalDelayDeltaEnabledFlag[i])
871
0
      {
872
0
        if ((bp.numCpbRemovalDelayDeltas - 1) > 0)
873
0
        {
874
0
          sei_read_code(pDecodedMessageOutputStream, (int)ceil(log2(bp.numCpbRemovalDelayDeltas)), symbol, "pt_cpb_removal_delay_delta_idx[i]");
875
0
          sei->cpbRemovalDelayDeltaIdx[i] = symbol;
876
0
        }
877
0
        else
878
0
        {
879
0
          sei->cpbRemovalDelayDeltaIdx[i] = 0;
880
0
        }
881
0
      }
882
0
      else
883
0
      {
884
0
        sei_read_code(pDecodedMessageOutputStream, bp.cpbRemovalDelayLength, symbol, "pt_cpb_removal_delay_minus1[i]");
885
0
        sei->auCpbRemovalDelay[i] = symbol + 1;
886
0
      }
887
0
    }
888
0
  }
889
0
  sei_read_code(pDecodedMessageOutputStream, bp.dpbOutputDelayLength, symbol, "pt_dpb_output_delay");
890
0
  sei->picDpbOutputDelay = symbol;
891
892
0
  if( bp.altCpbParamsPresentFlag )
893
0
  {
894
0
    sei_read_flag( pDecodedMessageOutputStream, symbol, "cpb_alt_timing_info_present_flag" ); sei->cpbAltTimingInfoPresentFlag = symbol;
895
0
    if( sei->cpbAltTimingInfoPresentFlag )
896
0
    {
897
0
      if (bp.bpNalCpbParamsPresentFlag)
898
0
      {
899
0
        for (int i = (bp.sublayerInitialCpbRemovalDelayPresentFlag ? 0 : bp.bpMaxSubLayers - 1);
900
0
             i <= bp.bpMaxSubLayers - 1; ++i)
901
0
        {
902
0
          for (int j = 0; j < bp.bpCpbCnt; j++)
903
0
          {
904
0
            sei_read_code(pDecodedMessageOutputStream, bp.initialCpbRemovalDelayLength, symbol,
905
0
                          "nal_cpb_alt_initial_cpb_removal_delay_delta[ i ][ j ]");
906
0
            sei->nalCpbAltInitialRemovalDelayDelta[i][j] = symbol;
907
0
            sei_read_code(pDecodedMessageOutputStream, bp.initialCpbRemovalDelayLength, symbol,
908
0
                          "nal_cpb_alt_initial_cpb_removal_offset_delta[ i ][ j ]");
909
0
            sei->nalCpbAltInitialRemovalOffsetDelta[i][j] = symbol;
910
0
          }
911
0
          sei_read_code(pDecodedMessageOutputStream, bp.cpbRemovalDelayLength, sei->nalCpbDelayOffset[i],
912
0
                        "nal_cpb_delay_offset[ i ]");
913
0
          sei_read_code(pDecodedMessageOutputStream, bp.dpbOutputDelayLength, sei->nalDpbDelayOffset[i],
914
0
                        "nal_dpb_delay_offset[ i ]");
915
0
        }
916
0
      }
917
918
0
      if (bp.bpVclCpbParamsPresentFlag)
919
0
      {
920
0
        for (int i = (bp.sublayerInitialCpbRemovalDelayPresentFlag ? 0 : bp.bpMaxSubLayers - 1);
921
0
             i <= bp.bpMaxSubLayers - 1; ++i)
922
0
        {
923
0
          for (int j = 0; j < bp.bpCpbCnt; j++)
924
0
          {
925
0
            sei_read_code(pDecodedMessageOutputStream, bp.initialCpbRemovalDelayLength, symbol,
926
0
                          "vcl_cpb_alt_initial_cpb_removal_delay_delta[ i ][ j ]");
927
0
            sei->vclCpbAltInitialRemovalDelayDelta[i][j] = symbol;
928
0
            sei_read_code(pDecodedMessageOutputStream, bp.initialCpbRemovalDelayLength, symbol,
929
0
                          "vcl_cpb_alt_initial_cpb_removal_offset_delta[ i ][ j ]");
930
0
            sei->vclCpbAltInitialRemovalOffsetDelta[i][j] = symbol;
931
0
          }
932
0
          sei_read_code(pDecodedMessageOutputStream, bp.cpbRemovalDelayLength, sei->vclCpbDelayOffset[i],
933
0
                        "vcl_cpb_delay_offset[ i ]");
934
0
          sei_read_code(pDecodedMessageOutputStream, bp.dpbOutputDelayLength, sei->vclDpbDelayOffset[i],
935
0
                        "vcl_dpb_delay_offset[ i ]");
936
0
        }
937
0
      }
938
0
    }
939
0
  }
940
0
  else
941
0
  {
942
0
    sei->cpbAltTimingInfoPresentFlag = false;
943
0
  }
944
945
0
  if ( bp.bpDecodingUnitHrdParamsPresentFlag && bp.decodingUnitDpbDuParamsInPicTimingSeiFlag )
946
0
  {
947
0
    sei_read_code( pDecodedMessageOutputStream, bp.dpbOutputDelayDuLength, symbol, "pic_dpb_output_du_delay" );
948
0
    sei->picDpbOutputDuDelay = symbol;
949
0
  }
950
0
  if( bp.bpDecodingUnitHrdParamsPresentFlag && bp.decodingUnitCpbParamsInPicTimingSeiFlag )
951
0
  {
952
0
    sei_read_uvlc( pDecodedMessageOutputStream, symbol, "num_decoding_units_minus1" );
953
0
    sei->numDecodingUnits = symbol+1;
954
0
    CHECK( sei->numDecodingUnits > 32, "num_decoding_units_minus1 must be <= 31 in vvdecSEIPictureTiming" );
955
956
0
    if (sei->numDecodingUnits > 1)
957
0
    {
958
0
    sei_read_flag( pDecodedMessageOutputStream, symbol, "du_common_cpb_removal_delay_flag" );
959
0
    sei->duCommonCpbRemovalDelayFlag = symbol;
960
0
    if( sei->duCommonCpbRemovalDelayFlag )
961
0
    {
962
0
      for( int i = temporalId; i < bp.bpMaxSubLayers; i ++ )
963
0
      {
964
0
        if( sei->ptSubLayerDelaysPresentFlag[i] )
965
0
        {
966
0
          sei_read_code( pDecodedMessageOutputStream, bp.duCpbRemovalDelayIncrementLength, symbol, "du_common_cpb_removal_delay_increment_minus1[i]" );
967
0
          sei->duCommonCpbRemovalDelay[i] = symbol+1;
968
0
        }
969
0
      }
970
0
    }
971
0
    for( uint32_t i = 0; i < sei->numDecodingUnits; i ++ )
972
0
    {
973
0
      sei_read_uvlc( pDecodedMessageOutputStream, symbol, "num_nalus_in_du_minus1[i]" );
974
0
      sei->numNalusInDu[i] = symbol+1;
975
0
      if( !sei->duCommonCpbRemovalDelayFlag && i < (sei->numDecodingUnits-1) )
976
0
      {
977
0
        for( int j = temporalId; j < bp.bpMaxSubLayers; j ++ )
978
0
        {
979
0
          if( sei->ptSubLayerDelaysPresentFlag[j] )
980
0
          {
981
0
            sei_read_code( pDecodedMessageOutputStream, bp.duCpbRemovalDelayIncrementLength, symbol, "du_cpb_removal_delay_increment_minus1[i][j]" );
982
0
            sei->duCpbRemovalDelay[i * bp.bpMaxSubLayers + j] = symbol+1;
983
0
          }
984
0
        }
985
0
      }
986
0
    }
987
0
    }
988
0
    else
989
0
    {
990
0
      sei->duCommonCpbRemovalDelayFlag = 0;
991
0
    }
992
0
  }
993
0
  if( bp.additionalConcatenationInfoPresentFlag )
994
0
  {
995
    // not stored
996
0
    sei_read_flag( pDecodedMessageOutputStream, symbol, "pt_delay_for_concatenation_ensured_flag" );
997
0
  }
998
0
  sei_read_code( pDecodedMessageOutputStream, 8, symbol, "pt_display_elemental_periods_minus1" );
999
0
  sei->ptDisplayElementalPeriods = symbol+1;
1000
0
}
1001
1002
void SEIReader::xParseSEIFrameFieldinfo(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1003
0
{
1004
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIFrameFieldInfo" );
1005
1006
0
  uint32_t symbol;
1007
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1008
1009
0
  vvdecSEIFrameFieldInfo* sei =(vvdecSEIFrameFieldInfo*)s->payload;
1010
0
  ::memset(sei, 0, sizeof(vvdecSEIFrameFieldInfo));
1011
1012
0
  sei_read_flag( pDecodedMessageOutputStream, symbol,      "field_pic_flag" );
1013
0
  sei->fieldPicFlag= symbol;
1014
0
  if (sei->fieldPicFlag)
1015
0
  {
1016
0
    sei_read_flag( pDecodedMessageOutputStream, symbol,    "bottofield_flag" );
1017
0
    sei->bottomFieldFlag = symbol;
1018
0
    sei_read_flag( pDecodedMessageOutputStream, symbol,    "pairing_indicated_flag" );
1019
0
    sei->pairingIndicatedFlag = symbol;
1020
0
    if (sei->pairingIndicatedFlag)
1021
0
    {
1022
0
      sei_read_flag( pDecodedMessageOutputStream, symbol,  "paired_with_next_field_flag" );
1023
0
      sei->pairedWithNextFieldFlag = symbol;
1024
0
    }
1025
0
  }
1026
0
  else
1027
0
  {
1028
0
    sei_read_flag( pDecodedMessageOutputStream, symbol,    "display_fields_froframe_flag" );
1029
0
    sei->displayFieldsFromFrameFlag = symbol;
1030
0
    if (sei->displayFieldsFromFrameFlag)
1031
0
    {
1032
0
      sei_read_flag( pDecodedMessageOutputStream, symbol,  "display_fields_froframe_flag" );
1033
0
      sei->topFieldFirstFlag = symbol;
1034
0
    }
1035
0
    sei_read_code( pDecodedMessageOutputStream, 8, symbol, "ffi_display_elemental_periods_minus1" );
1036
0
    sei->displayElementalPeriods = symbol+1;
1037
0
  }
1038
0
  sei_read_code( pDecodedMessageOutputStream, 2, symbol,   "source_scan_type" );
1039
0
  sei->sourceScanType = symbol;
1040
0
  sei_read_flag( pDecodedMessageOutputStream, symbol,      "duplicate_flag" );
1041
0
  sei->duplicateFlag = symbol;
1042
0
}
1043
1044
void SEIReader::xParseSEIDependentRAPIndication( vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream )
1045
0
{
1046
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSeidependent_rap_indication_t" );
1047
1048
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1049
0
}
1050
1051
void SEIReader::xParseSEIFramePacking(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1052
0
{
1053
0
  uint32_t val;
1054
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIFramePacking" );
1055
1056
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1057
1058
0
  vvdecSEIFramePacking* sei =(vvdecSEIFramePacking*)s->payload;
1059
0
  ::memset(sei, 0, sizeof(vvdecSEIFramePacking));
1060
1061
0
  sei_read_uvlc( pDecodedMessageOutputStream, val, "frame_packing_arrangement_id" );                 sei->arrangementId = val;
1062
0
  sei_read_flag( pDecodedMessageOutputStream, val, "frame_packing_arrangement_cancel_flag" );        sei->arrangementCancelFlag = val;
1063
1064
0
  if( !sei->arrangementCancelFlag )
1065
0
  {
1066
0
    sei_read_code( pDecodedMessageOutputStream, 7, val, "frame_packing_arrangement_type" );          sei->arrangementType = val;
1067
0
    CHECK( ( sei->arrangementType <= 2 ) || ( sei->arrangementType >= 6 ), "Invalid arrangement type" );
1068
1069
0
    sei_read_flag( pDecodedMessageOutputStream, val, "quincunx_sampling_flag" );                     sei->quincunxSamplingFlag = val;
1070
1071
0
    sei_read_code( pDecodedMessageOutputStream, 6, val, "content_interpretation_type" );             sei->contentInterpretationType = val;
1072
0
    sei_read_flag( pDecodedMessageOutputStream, val, "spatial_flipping_flag" );                      sei->spatialFlippingFlag = val;
1073
0
    sei_read_flag( pDecodedMessageOutputStream, val, "frame0_flipped_flag" );                        sei->frame0FlippedFlag = val;
1074
0
    sei_read_flag( pDecodedMessageOutputStream, val, "field_views_flag" );                           sei->fieldViewsFlag = val;
1075
0
    sei_read_flag( pDecodedMessageOutputStream, val, "current_frame_is_frame0_flag" );               sei->currentFrameIsFrame0Flag = val;
1076
0
    sei_read_flag( pDecodedMessageOutputStream, val, "frame0_self_contained_flag" );                 sei->frame0SelfContainedFlag = val;
1077
0
    sei_read_flag( pDecodedMessageOutputStream, val, "frame1_self_contained_flag" );                 sei->frame1SelfContainedFlag = val;
1078
1079
0
    if ( sei->quincunxSamplingFlag == 0 && sei->arrangementType != 5)
1080
0
    {
1081
0
      sei_read_code( pDecodedMessageOutputStream, 4, val, "frame0_grid_position_x" );                sei->frame0GridPositionX = val;
1082
0
      sei_read_code( pDecodedMessageOutputStream, 4, val, "frame0_grid_position_y" );                sei->frame0GridPositionY = val;
1083
0
      sei_read_code( pDecodedMessageOutputStream, 4, val, "frame1_grid_position_x" );                sei->frame1GridPositionX = val;
1084
0
      sei_read_code( pDecodedMessageOutputStream, 4, val, "frame1_grid_position_y" );                sei->frame1GridPositionY = val;
1085
0
    }
1086
1087
0
    sei_read_code( pDecodedMessageOutputStream, 8, val, "frame_packing_arrangement_reserved_byte" ); sei->arrangementReservedByte = val;
1088
0
    sei_read_flag( pDecodedMessageOutputStream, val,  "frame_packing_arrangement_persistence_flag" );sei->arrangementPersistenceFlag = (val != 0);
1089
0
  }
1090
0
  sei_read_flag( pDecodedMessageOutputStream, val, "upsampled_aspect_ratio_flag" );                  sei->upsampledAspectRatio = val;
1091
0
}
1092
1093
void SEIReader::xParseSEIParameterSetsInclusionIndication(vvdecSEI* s, uint32_t payloadSize, std::ostream* pDecodedMessageOutputStream)
1094
0
{
1095
0
  uint32_t val;
1096
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIParameterSetsInclusionIndication" );
1097
1098
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1099
1100
0
  vvdecSEIParameterSetsInclusionIndication* sei =(vvdecSEIParameterSetsInclusionIndication*)s->payload;
1101
1102
0
  sei_read_flag( pDecodedMessageOutputStream, val, "self_contained_clvs_flag" ); sei->selfContainedClvsFlag = val;
1103
0
}
1104
1105
void SEIReader::xParseSEIMasteringDisplayColourVolume( vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1106
0
{
1107
0
  uint32_t code;
1108
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIMasteringDisplayColourVolume" );
1109
1110
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1111
  
1112
0
  vvdecSEIMasteringDisplayColourVolume* sei =(vvdecSEIMasteringDisplayColourVolume*)s->payload;
1113
0
  ::memset(sei, 0, sizeof(vvdecSEIMasteringDisplayColourVolume));
1114
1115
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "display_primaries_x[0]" ); sei->primaries[0][0] = code;
1116
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "display_primaries_y[0]" ); sei->primaries[0][1] = code;
1117
1118
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "display_primaries_x[1]" ); sei->primaries[1][0] = code;
1119
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "display_primaries_y[1]" ); sei->primaries[1][1] = code;
1120
1121
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "display_primaries_x[2]" ); sei->primaries[2][0] = code;
1122
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "display_primaries_y[2]" ); sei->primaries[2][1] = code;
1123
1124
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "white_point_x" ); sei->whitePoint[0] = code;
1125
0
  sei_read_code( pDecodedMessageOutputStream, 16, code, "white_point_y" ); sei->whitePoint[1] = code;
1126
1127
0
  sei_read_code( pDecodedMessageOutputStream, 32, code, "max_display_mastering_luminance" ); sei->maxLuminance = code;
1128
0
  sei_read_code( pDecodedMessageOutputStream, 32, code, "min_display_mastering_luminance" ); sei->minLuminance = code;
1129
0
}
1130
1131
#if U0033_ALTERNATIVE_TRANSFER_CHARACTERISTICS_SEI
1132
void SEIReader::xParseSEIAlternativeTransferCharacteristics(vvdecSEI* s, uint32_t payloadSize, std::ostream* pDecodedMessageOutputStream)
1133
0
{
1134
0
  uint32_t code;
1135
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIAlternativeTransferCharacteristics" );
1136
1137
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1138
  
1139
0
  vvdecSEIAlternativeTransferCharacteristics* sei =(vvdecSEIAlternativeTransferCharacteristics*)s->payload;
1140
1141
0
  sei_read_code(pDecodedMessageOutputStream, 8, code, "preferred_transfer_characteristics"); sei->preferred_transfer_characteristics = code;
1142
0
}
1143
#endif
1144
void SEIReader::xParseSEIUserDataRegistered(vvdecSEI* sei, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1145
0
{
1146
0
  uint32_t code;
1147
0
  CHECK_FATAL( !sei || sei->payload == NULL, "allocation error in vvdecSEIUserDataRegistered" );
1148
1149
0
  output_sei_message_header(sei, pDecodedMessageOutputStream, payloadSize);
1150
1151
0
  vvdecSEIUserDataRegistered t;
1152
1153
0
  CHECK(payloadSize == 0, "no payload" );
1154
0
  sei_read_code(pDecodedMessageOutputStream, 8, code, "itu_t_t35_country_code"); payloadSize--;
1155
0
  if (code == 255)
1156
0
  {
1157
0
    CHECK(payloadSize == 0, "no payload" );
1158
0
    sei_read_code(pDecodedMessageOutputStream, 8, code, "itu_t_t35_country_code_extension_byte"); payloadSize--;
1159
0
    code += 255;
1160
0
  }
1161
0
  t.ituCountryCode = code;
1162
0
  t.userDataLength = payloadSize;
1163
1164
0
  if( !payloadSize )
1165
0
  {
1166
0
    t.userData = 0;
1167
0
    ::memcpy(sei->payload, &t, sizeof(vvdecSEIUserDataRegistered));
1168
0
    return;
1169
0
  }
1170
1171
0
  if( sei->payload )
1172
0
      free( sei->payload );
1173
1174
0
  SEI_internal::allocSEIPayload( sei, sizeof( vvdecSEIUserDataRegistered ) + sizeof( uint8_t )*(t.userDataLength + 1) );
1175
0
  CHECK_FATAL( sei->payload == NULL, "allocation error in vvdecSEIUserDataRegistered" );
1176
1177
0
  vvdecSEIUserDataRegistered* target = (vvdecSEIUserDataRegistered*)sei->payload;
1178
0
  target->ituCountryCode = t.ituCountryCode;
1179
0
  target->userDataLength = t.userDataLength;
1180
0
  target->userData = (uint8_t*)((uint8_t*)sei->payload + sizeof( vvdecSEIUserDataRegistered ));
1181
1182
0
  for (uint32_t i = 0; i < target->userDataLength; i++)
1183
0
  {
1184
0
    sei_read_code(NULL, 8, code, "itu_t_t35_payload_byte");
1185
0
    target->userData[i] = code;
1186
0
  }
1187
1188
0
  if (pDecodedMessageOutputStream)
1189
0
  {
1190
0
    (*pDecodedMessageOutputStream) << "  itu_t_t35 payload size: " << target->userDataLength << "\n";
1191
0
  }
1192
0
}
1193
1194
/**
1195
 * parse bitstream bs and unpack a user_data_unregistered SEI message
1196
 * of payloasSize bytes into sei.
1197
 */
1198
void SEIReader::xParseSEIuserDataUnregistered(vvdecSEI* sei, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1199
0
{
1200
0
  CHECK(payloadSize < 16, "Payload too small");
1201
0
  CHECK_FATAL( !sei || sei->payload == NULL, "allocation error in vvdecSEIUserDataUnregistered" );
1202
1203
0
  uint32_t val;
1204
1205
0
  output_sei_message_header(sei, pDecodedMessageOutputStream, payloadSize);
1206
1207
0
  vvdecSEIUserDataUnregistered t;
1208
1209
0
  for (uint32_t i = 0; i < 16; i++)
1210
0
  {
1211
0
    sei_read_code( pDecodedMessageOutputStream, 8, val, "uuid_iso_iec_11578"); t.uuid_iso_iec_11578[i] = val;
1212
0
  }
1213
1214
0
  t.userDataLength = payloadSize - 16;
1215
0
  if (!t.userDataLength)
1216
0
  {
1217
0
    t.userData = 0;
1218
0
    ::memcpy(sei->payload, &t, sizeof(vvdecSEIUserDataUnregistered));
1219
0
    return;
1220
0
  }
1221
1222
0
  if( sei->payload )
1223
0
      free( sei->payload );
1224
1225
0
  SEI_internal::allocSEIPayload( sei,  sizeof( vvdecSEIUserDataUnregistered ) + sizeof( uint8_t )*(t.userDataLength + 1) );
1226
0
  CHECK_FATAL( sei->payload == NULL, "allocation error in vvdecSEIUserDataUnregistered" );
1227
1228
0
  vvdecSEIUserDataUnregistered* target = (vvdecSEIUserDataUnregistered*)sei->payload;
1229
0
  target->userDataLength = t.userDataLength;
1230
0
  target->userData = (uint8_t*)((uint8_t*)sei->payload + sizeof( vvdecSEIUserDataUnregistered ));
1231
1232
0
  for (uint32_t i = 0; i < target->userDataLength; i++)
1233
0
  {
1234
0
    sei_read_code(NULL, 8, val, "user_data_payload_byte");
1235
0
    target->userData[i] = val;
1236
0
  }
1237
1238
0
  if (pDecodedMessageOutputStream)
1239
0
  {
1240
0
    (*pDecodedMessageOutputStream) << "  User data payload size: " << target->userDataLength << "\n";
1241
0
  }
1242
0
}
1243
1244
void SEIReader::xParseSEIFilmGrainCharacteristics(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1245
0
{
1246
0
  uint32_t code;
1247
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIFilmGrainCharacteristics" );
1248
1249
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1250
1251
0
  vvdecSEIFilmGrainCharacteristics* sei =(vvdecSEIFilmGrainCharacteristics*)s->payload;
1252
0
  ::memset(sei, 0, sizeof(vvdecSEIFilmGrainCharacteristics));
1253
1254
0
  sei_read_flag(pDecodedMessageOutputStream, code, "filgrain_characteristics_cancel_flag");     sei->filmGrainCharacteristicsCancelFlag = code != 0;
1255
0
  if (!sei->filmGrainCharacteristicsCancelFlag)
1256
0
  {
1257
0
    sei_read_code(pDecodedMessageOutputStream, 2, code, "filgrain_model_id");                   sei->filmGrainModelId = code;
1258
0
    sei_read_flag(pDecodedMessageOutputStream, code, "separate_colour_description_present_flag"); sei->separateColourDescriptionPresentFlag = code != 0;
1259
0
    if (sei->separateColourDescriptionPresentFlag)
1260
0
    {
1261
0
      sei_read_code(pDecodedMessageOutputStream, 3, code, "filgrain_bit_depth_luma_minus8");    sei->filmGrainBitDepthLuma = code+8;
1262
0
      sei_read_code(pDecodedMessageOutputStream, 3, code, "filgrain_bit_depth_chroma_minus8");  sei->filmGrainBitDepthChroma = code+8;
1263
0
      sei_read_flag(pDecodedMessageOutputStream, code, "filgrain_full_range_flag");             sei->filmGrainFullRangeFlag = code != 0;
1264
0
      sei_read_code(pDecodedMessageOutputStream, 8, code, "filgrain_colour_primaries");         sei->filmGrainColourPrimaries = code;
1265
0
      sei_read_code(pDecodedMessageOutputStream, 8, code, "filgrain_transfer_characteristics"); sei->filmGrainTransferCharacteristics = code;
1266
0
      sei_read_code(pDecodedMessageOutputStream, 8, code, "filgrain_matrix_coeffs");            sei->filmGrainMatrixCoeffs = code;
1267
0
    }
1268
0
    sei_read_code(pDecodedMessageOutputStream, 2, code, "blending_mode_id");                      sei->blendingModeId = code;
1269
0
    sei_read_code(pDecodedMessageOutputStream, 4, code, "log2_scale_factor");                     sei->log2ScaleFactor = code;
1270
0
    for (int c = 0; c<3; c++)
1271
0
    {
1272
0
      sei_read_flag(pDecodedMessageOutputStream, code, "comp_model_present_flag[c]");             sei->compModel[c].presentFlag = code != 0;
1273
0
    }
1274
0
    for (int c = 0; c<3; c++)
1275
0
    {
1276
0
      vvdecCompModel &cm = sei->compModel[c];
1277
0
      if (cm.presentFlag)
1278
0
      {
1279
0
        sei_read_code(pDecodedMessageOutputStream, 8, code, "num_intensity_intervals_minus1[c]"); cm.numIntensityIntervals = code + 1;
1280
0
        sei_read_code(pDecodedMessageOutputStream, 3, code, "num_model_values_minus1[c]");        cm.numModelValues = code + 1;
1281
1282
0
        CHECK ( cm.numIntensityIntervals > 256, "num_intensity_intervals_minus1[c] out of range" );
1283
0
        CHECK ( cm.numModelValues > 6, "num_model_values_minus1[c] out of range" );
1284
1285
0
        for (uint32_t interval = 0; interval < cm.numIntensityIntervals; interval++)
1286
0
        {
1287
0
          vvdecCompModelIntensityValues &cmiv = cm.intensityValues[interval];
1288
0
          sei_read_code(pDecodedMessageOutputStream, 8, code, "intensity_interval_lower_bound[c][i]"); cmiv.intensityIntervalLowerBound = code;
1289
0
          sei_read_code(pDecodedMessageOutputStream, 8, code, "intensity_interval_upper_bound[c][i]"); cmiv.intensityIntervalUpperBound = code;
1290
0
          for (uint32_t j = 0; j<cm.numModelValues; j++)
1291
0
          {
1292
0
            sei_read_svlc(pDecodedMessageOutputStream, cmiv.compModelValue[j], "comp_model_value[c][i]");
1293
0
          }
1294
0
        }
1295
0
      }
1296
0
    } // for c
1297
0
    sei_read_flag(pDecodedMessageOutputStream, code, "filgrain_characteristics_persistence_flag"); sei->filmGrainCharacteristicsPersistenceFlag = code != 0;
1298
0
  } // cancel flag
1299
0
}
1300
1301
void SEIReader::xParseSEIContentLightLevelInfo(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1302
0
{
1303
0
  uint32_t code;
1304
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIContentLightLevelInfo" );
1305
1306
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1307
1308
0
  vvdecSEIContentLightLevelInfo* sei =(vvdecSEIContentLightLevelInfo*)s->payload;
1309
0
  ::memset(sei, 0, sizeof(vvdecSEIContentLightLevelInfo));
1310
1311
0
  sei_read_code(pDecodedMessageOutputStream, 16, code, "max_content_light_level");     sei->maxContentLightLevel    = code;
1312
0
  sei_read_code(pDecodedMessageOutputStream, 16, code, "max_pic_average_light_level"); sei->maxPicAverageLightLevel = code;
1313
0
}
1314
1315
void SEIReader::xParseSEIAmbientViewingEnvironment(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1316
0
{
1317
0
  uint32_t code;
1318
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIAmbientViewingEnvironment" );
1319
1320
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1321
1322
0
  vvdecSEIAmbientViewingEnvironment* sei =(vvdecSEIAmbientViewingEnvironment*)s->payload;
1323
0
  ::memset(sei, 0, sizeof(vvdecSEIAmbientViewingEnvironment));
1324
1325
0
  sei_read_code(pDecodedMessageOutputStream, 32, code, "ambient_illuminance"); sei->ambientIlluminance = code;
1326
0
  sei_read_code(pDecodedMessageOutputStream, 16, code, "ambient_light_x");     sei->ambientLightX = (uint16_t)code;
1327
0
  sei_read_code(pDecodedMessageOutputStream, 16, code, "ambient_light_y");     sei->ambientLightY = (uint16_t)code;
1328
0
}
1329
1330
void SEIReader::xParseSEIContentColourVolume(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1331
0
  {
1332
0
  int i;
1333
0
  uint32_t val;
1334
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIContentColourVolume" );
1335
1336
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1337
1338
0
  vvdecSEIContentColourVolume* sei =(vvdecSEIContentColourVolume*)s->payload;
1339
0
  ::memset(sei, 0, sizeof(vvdecSEIContentColourVolume));
1340
1341
0
  sei_read_flag(pDecodedMessageOutputStream, val, "ccv_cancel_flag");          sei->ccvCancelFlag = val;
1342
0
  if (!sei->ccvCancelFlag)
1343
0
    {
1344
0
    int iVal;
1345
0
    sei_read_flag(pDecodedMessageOutputStream, val, "ccv_persistence_flag");                   sei->ccvPersistenceFlag = val;
1346
0
    sei_read_flag(pDecodedMessageOutputStream, val, "ccv_primaries_present_flag");             sei->ccvPrimariesPresentFlag = val;
1347
0
    sei_read_flag(pDecodedMessageOutputStream, val, "ccv_min_luminance_value_present_flag");   sei->ccvMinLuminanceValuePresentFlag = val;
1348
0
    sei_read_flag(pDecodedMessageOutputStream, val, "ccv_max_luminance_value_present_flag");   sei->ccvMaxLuminanceValuePresentFlag = val;
1349
0
    sei_read_flag(pDecodedMessageOutputStream, val, "ccv_avg_luminance_value_present_flag");   sei->ccvAvgLuminanceValuePresentFlag = val;
1350
1351
0
    if (sei->ccvPrimariesPresentFlag)
1352
0
    {
1353
0
      for (i = 0; i < MAX_NUM_COMPONENT; i++)
1354
0
    {
1355
0
        sei_read_scode(pDecodedMessageOutputStream, 32, iVal, "ccv_primaries_x[i]");          sei->ccvPrimariesX[i] = iVal;
1356
0
        sei_read_scode(pDecodedMessageOutputStream, 32, iVal, "ccv_primaries_y[i]");          sei->ccvPrimariesY[i] = iVal;
1357
0
    }
1358
0
  }
1359
0
    if (sei->ccvMinLuminanceValuePresentFlag)
1360
0
  {
1361
0
      sei_read_code(pDecodedMessageOutputStream, 32, val, "ccv_min_luminance_value");   sei->ccvMinLuminanceValue = val;
1362
0
    }
1363
0
    if (sei->ccvMaxLuminanceValuePresentFlag)
1364
0
    {
1365
0
      sei_read_code(pDecodedMessageOutputStream, 32, val, "ccv_max_luminance_value");   sei->ccvMaxLuminanceValue = val;
1366
0
    }
1367
0
    if (sei->ccvAvgLuminanceValuePresentFlag)
1368
0
    {
1369
0
      sei_read_code(pDecodedMessageOutputStream, 32, val, "ccv_avg_luminance_value");   sei->ccvAvgLuminanceValue = val;
1370
0
    }
1371
0
  }
1372
0
}
1373
void SEIReader::xParseSEIEquirectangularProjection(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1374
0
{
1375
0
  uint32_t val;
1376
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIEquirectangularProjection" );
1377
1378
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1379
1380
0
  vvdecSEIEquirectangularProjection* sei =(vvdecSEIEquirectangularProjection*)s->payload;
1381
0
  ::memset(sei, 0, sizeof(vvdecSEIEquirectangularProjection));
1382
1383
0
  sei_read_flag( pDecodedMessageOutputStream, val,       "erp_cancel_flag" );              sei->erpCancelFlag = val;
1384
0
  if( !sei->erpCancelFlag )
1385
0
  {
1386
0
    sei_read_flag( pDecodedMessageOutputStream, val,      "erp_persistence_flag"    );     sei->erpPersistenceFlag   = val;
1387
0
    sei_read_flag( pDecodedMessageOutputStream, val,      "erp_guard_band_flag"     );     sei->erpGuardBandFlag     = val;
1388
0
    sei_read_code( pDecodedMessageOutputStream, 2, val,   "erp_reserved_zero_2bits" );
1389
0
    if ( sei->erpGuardBandFlag == 1)
1390
0
    {
1391
0
      sei_read_code( pDecodedMessageOutputStream, 3, val,     "erp_guard_band_type"       );   sei->erpGuardBandType  = val;
1392
0
      sei_read_code( pDecodedMessageOutputStream, 8, val,     "erp_left_guard_band_width" );   sei->erpLeftGuardBandWidth = val;
1393
0
      sei_read_code( pDecodedMessageOutputStream, 8, val,     "erp_right_guard_band_width");   sei->erpRightGuardBandWidth = val;
1394
0
    }
1395
0
  }
1396
0
}
1397
1398
void SEIReader::xParseSEISphereRotation(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1399
0
{
1400
0
  uint32_t val;
1401
0
  int  sval;
1402
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEISphereRotation" );
1403
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1404
1405
0
  vvdecSEISphereRotation* sei =(vvdecSEISphereRotation*)s->payload;
1406
0
  ::memset(sei, 0, sizeof(vvdecSEISphereRotation));
1407
1408
0
  sei_read_flag( pDecodedMessageOutputStream, val,       "sphere_rotation_cancel_flag" );              sei->sphereRotationCancelFlag = val;
1409
0
  if( !sei->sphereRotationCancelFlag )
1410
0
    {
1411
0
    sei_read_flag ( pDecodedMessageOutputStream,      val,   "sphere_rotation_persistence_flag"    );     sei->sphereRotationPersistenceFlag = val;
1412
0
    sei_read_code ( pDecodedMessageOutputStream, 6,   val,   "sphere_rotation_reserved_zero_6bits" );
1413
0
    sei_read_scode( pDecodedMessageOutputStream, 32, sval,   "sphere_rotation_yaw"                 );     sei->sphereRotationYaw = sval;
1414
0
    sei_read_scode( pDecodedMessageOutputStream, 32, sval,   "sphere_rotation_pitch"               );     sei->sphereRotationPitch = sval;
1415
0
    sei_read_scode( pDecodedMessageOutputStream, 32, sval,   "sphere_rotation_roll"                );     sei->sphereRotationRoll = sval;
1416
0
  }
1417
0
}
1418
1419
void SEIReader::xParseSEIOmniViewport(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1420
0
{
1421
0
  uint32_t code;
1422
0
  int  scode;
1423
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIOmniViewport" );
1424
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1425
1426
0
  vvdecSEIOmniViewport* sei =(vvdecSEIOmniViewport*)s->payload;
1427
0
  ::memset(sei, 0, sizeof(vvdecSEIOmniViewport));
1428
1429
0
  sei_read_code( pDecodedMessageOutputStream, 10, code, "omni_viewport_id"          ); sei->omniViewportId         = code;
1430
0
  sei_read_flag( pDecodedMessageOutputStream,     code, "omni_viewport_cancel_flag" ); sei->omniViewportCancelFlag = code;
1431
1432
0
  if (!sei->omniViewportCancelFlag)
1433
0
  {
1434
0
    sei_read_flag( pDecodedMessageOutputStream,    code, "omni_viewport_persistence_flag" );  sei->omniViewportPersistenceFlag = code;
1435
0
    sei_read_code( pDecodedMessageOutputStream, 4, code, "omni_viewport_cnt_minus1"       );  sei->omniViewportCnt =  code+1;
1436
0
    CHECK( sei->omniViewportCnt > 16, "omni_viewport_cnt_minus1 must be < 16 in vvdecSEIOmniViewport" );
1437
1438
0
    for(uint32_t region=0; region < sei->omniViewportCnt; region++)
1439
0
    {
1440
0
      vvdecOmniViewportRegion &viewport = sei->omniViewportRegions[region];
1441
0
      sei_read_scode( pDecodedMessageOutputStream, 32, scode, "omni_viewport_azimuth_centre"   );   viewport.azimuthCentre   = scode;
1442
0
      sei_read_scode( pDecodedMessageOutputStream, 32, scode, "omni_viewport_elevation_centre" );   viewport.elevationCentre = scode;
1443
0
      sei_read_scode( pDecodedMessageOutputStream, 32, scode, "omni_viewport_tilt_centre"      );   viewport.tiltCentre      = scode;
1444
0
      sei_read_code( pDecodedMessageOutputStream,  32, code , "omni_viewport_hor_range"        );   viewport.horRange        = code;
1445
0
      sei_read_code( pDecodedMessageOutputStream,  32, code , "omni_viewport_ver_range"        );   viewport.verRange        = code;
1446
0
    }
1447
0
  }
1448
0
  else
1449
0
  {
1450
0
    sei->omniViewportPersistenceFlag=false;
1451
0
  }
1452
0
}
1453
1454
void SEIReader::xParseSEIRegionWisePacking(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1455
0
{
1456
0
  uint32_t val;
1457
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIRegionWisePacking" );
1458
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1459
1460
0
  vvdecSEIRegionWisePacking* sei =(vvdecSEIRegionWisePacking*)s->payload;
1461
0
  ::memset(sei, 0, sizeof(vvdecSEIRegionWisePacking));
1462
1463
0
  sei_read_flag( pDecodedMessageOutputStream,             val,    "rwp_cancel_flag" );                      sei->rwpCancelFlag = val;
1464
0
  if (!sei->rwpCancelFlag)
1465
0
    {
1466
0
    sei_read_flag( pDecodedMessageOutputStream,           val,    "rwp_persistence_flag" );                 sei->rwpPersistenceFlag = val;
1467
0
    sei_read_flag( pDecodedMessageOutputStream,           val,    "constituent_picture_matching_flag" );    sei->constituentPictureMatchingFlag = val;
1468
0
    sei_read_code( pDecodedMessageOutputStream,       5,  val,    "rwp_reserved_zero_5bits" );
1469
0
    sei_read_code( pDecodedMessageOutputStream,       8,  val,    "num_packed_regions" );                   sei->numPackedRegions    = val;
1470
0
    sei_read_code( pDecodedMessageOutputStream,       32, val,    "proj_picture_width" );                   sei->projPictureWidth    = val;
1471
0
    sei_read_code( pDecodedMessageOutputStream,       32, val,    "proj_picture_height" );                  sei->projPictureHeight   = val;
1472
0
    sei_read_code( pDecodedMessageOutputStream,       16, val,    "packed_picture_width" );                 sei->packedPictureWidth  = val;
1473
0
    sei_read_code( pDecodedMessageOutputStream,       16, val,    "packed_picture_height" );                sei->packedPictureHeight = val;
1474
1475
0
    for( int i=0; i < sei->numPackedRegions; i++ )
1476
0
    {
1477
0
      sei_read_code( pDecodedMessageOutputStream,     4,  val,    "rwp_reserved_zero_4bits" );
1478
0
      sei_read_code( pDecodedMessageOutputStream,     3,  val,    "rwp_tTransfortype" );                  sei->rwpTransformType[i]   = val;
1479
0
      sei_read_flag( pDecodedMessageOutputStream,         val,    "rwp_guard_band_flag" );                  sei->rwpGuardBandFlag[i]   = val;
1480
0
      sei_read_code( pDecodedMessageOutputStream,     32, val,    "proj_region_width" );                    sei->projRegionWidth[i]    = val;
1481
0
      sei_read_code( pDecodedMessageOutputStream,     32, val,    "proj_region_height" );                   sei->projRegionHeight[i]   = val;
1482
0
      sei_read_code( pDecodedMessageOutputStream,     32, val,    "rwp_proj_regionTop" );                   sei->rwpProjRegionTop[i]   = val;
1483
0
      sei_read_code( pDecodedMessageOutputStream,     32, val,    "proj_region_left" );                     sei->projRegionLeft[i]     = val;
1484
0
      sei_read_code( pDecodedMessageOutputStream,     16, val,    "packed_region_width" );                  sei->packedRegionWidth[i]  = val;
1485
0
      sei_read_code( pDecodedMessageOutputStream,     16, val,    "packed_region_height" );                 sei->packedRegionHeight[i] = val;
1486
0
      sei_read_code( pDecodedMessageOutputStream,     16, val,    "packed_region_top" );                    sei->packedRegionTop[i]    = val;
1487
0
      sei_read_code( pDecodedMessageOutputStream,     16, val,    "packed_region_left" );                   sei->packedRegionLeft[i]   = val;
1488
0
      if( sei->rwpGuardBandFlag[i] )
1489
0
        {
1490
0
        sei_read_code( pDecodedMessageOutputStream,   8,  val,    "rwp_left_guard_band_width" );            sei->rwpLeftGuardBandWidth[i]     = val;
1491
0
        sei_read_code( pDecodedMessageOutputStream,   8,  val,    "rwp_right_guard_band_width" );           sei->rwpRightGuardBandWidth[i]    = val;
1492
0
        sei_read_code( pDecodedMessageOutputStream,   8,  val,    "rwp_top_guard_band_height" );            sei->rwpTopGuardBandHeight[i]     = val;
1493
0
        sei_read_code( pDecodedMessageOutputStream,   8,  val,    "rwp_bottoguard_band_height" );         sei->rwpBottomGuardBandHeight[i]  = val;
1494
0
        sei_read_flag( pDecodedMessageOutputStream,       val,    "rwp_guard_band_not_used_forPred_flag" ); sei->rwpGuardBandNotUsedForPredFlag[i] = val;
1495
0
        for( int j=0; j < 4; j++ )
1496
0
          {
1497
0
          sei_read_code( pDecodedMessageOutputStream, 3,  val,     "rwp_guard_band_type" ); sei->rwpGuardBandType[i*4 + j] = val;
1498
0
        }
1499
0
        sei_read_code( pDecodedMessageOutputStream,   3,  val,    "rwp_guard_band_reserved_zero_3bits" );
1500
0
      }
1501
0
    }
1502
0
  }
1503
0
}
1504
1505
void SEIReader::xParseSEIGeneralizedCubemapProjection(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1506
0
{
1507
0
  uint32_t val;
1508
1509
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEIGeneralizedCubemapProjection" );
1510
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1511
  
1512
0
  vvdecSEIGeneralizedCubemapProjection* sei =(vvdecSEIGeneralizedCubemapProjection*)s->payload;
1513
0
  ::memset(sei, 0, sizeof(vvdecSEIGeneralizedCubemapProjection));
1514
1515
0
  sei_read_flag( pDecodedMessageOutputStream,          val,    "gcmp_cancel_flag" );                      sei->gcmpCancelFlag = val;
1516
0
  if (!sei->gcmpCancelFlag)
1517
0
  {
1518
0
    sei_read_flag( pDecodedMessageOutputStream,        val,    "gcmp_persistence_flag"    );              sei->gcmpPersistenceFlag = val;
1519
0
    sei_read_code( pDecodedMessageOutputStream,     3, val,    "gcmp_packing_type" );                     sei->gcmpPackingType = val;
1520
0
    sei_read_code( pDecodedMessageOutputStream,     2, val,    "gcmp_mapping_function_type"     );        sei->gcmpMappingFunctionType = val;
1521
1522
0
    int numFace = sei->gcmpPackingType == 4 || sei->gcmpPackingType == 5 ? 5 : 6;
1523
1524
0
    for (int i = 0; i < numFace; i++)
1525
0
    {
1526
0
      sei_read_code( pDecodedMessageOutputStream,   3, val,    "gcmp_face_index" );                       sei->gcmpFaceIndex[i] = val;
1527
0
      sei_read_code( pDecodedMessageOutputStream,   2, val,    "gcmp_face_rotation" );                    sei->gcmpFaceRotation[i] = val;
1528
0
      if (sei->gcmpMappingFunctionType == 2)
1529
0
      {
1530
0
        sei_read_code( pDecodedMessageOutputStream, 7, val,    "gcmp_function_coeff_u" );                 sei->gcmpFunctionCoeffU[i] = val;
1531
0
        sei_read_flag( pDecodedMessageOutputStream,    val,    "gcmp_function_u_affected_by_v_flag"    ); sei->gcmpFunctionUAffectedByVFlag[i] = val;
1532
0
        sei_read_code( pDecodedMessageOutputStream, 7, val,    "gcmp_function_coeff_v" );                 sei->gcmpFunctionCoeffV[i] = val;
1533
0
        sei_read_flag( pDecodedMessageOutputStream,    val,    "gcmp_function_v_affected_by_u_flag"    ); sei->gcmpFunctionVAffectedByUFlag[i] = val;
1534
0
      }
1535
0
    }
1536
0
    sei_read_flag( pDecodedMessageOutputStream,        val,    "gcmp_guard_band_flag" );                  sei->gcmpGuardBandFlag = val;
1537
0
    if (sei->gcmpGuardBandFlag)
1538
0
    {
1539
0
      sei_read_code( pDecodedMessageOutputStream,   3, val,    "gcmp_guard_band_type" );                   sei->gcmpGuardBandType = val;
1540
0
      sei_read_flag( pDecodedMessageOutputStream,      val,    "gcmp_guard_band_boundary_exterior_flag" ); sei->gcmpGuardBandBoundaryExteriorFlag = val;
1541
0
      sei_read_code( pDecodedMessageOutputStream,   4, val,    "gcmp_guard_band_samples_minus1" );         sei->gcmpGuardBandSamples = val+1;
1542
0
    }
1543
0
  }
1544
0
}
1545
1546
void SEIReader::xParseSEISubpictureLevelInfo(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1547
0
{
1548
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEISubpictureLevelInfo" );
1549
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1550
0
  vvdecSEISubpictureLevelInfo* sei =(vvdecSEISubpictureLevelInfo*)s->payload;
1551
0
  ::memset(sei, 0, sizeof(vvdecSEISubpictureLevelInfo));
1552
1553
0
  uint32_t val;
1554
0
  sei_read_code( pDecodedMessageOutputStream,   3,  val,    "sli_nuref_levels_minus1" );              sei->numRefLevels  = val + 1;
1555
0
  CHECK( sei->numRefLevels > 6, "sli_nuref_levels_minus1 must be < 6 in vvdecSEISubpictureLevelInfo" );
1556
1557
0
  sei_read_flag( pDecodedMessageOutputStream,       val,    "sli_cbr_constraint_flag" );              sei->cbrConstraintFlag = val;
1558
0
  sei_read_flag( pDecodedMessageOutputStream,       val,    "sli_explicit_fraction_present_flag" );   sei->explicitFractionPresentFlag = val;
1559
0
  if (sei->explicitFractionPresentFlag)
1560
0
  {
1561
0
    sei_read_uvlc(pDecodedMessageOutputStream,      val,    "sli_nusubpics_minus1");                  sei->numSubpics = val + 1;
1562
0
    CHECK( sei->numSubpics < 0 || sei->numSubpics > 64, "sli_nusubpics_minus1 must be < 64 in vvdecSEISubpictureLevelInfo" );
1563
0
  }
1564
0
  sei_read_code(pDecodedMessageOutputStream,    3,  val,    "sli_max_sublayers_minus1"  );            sei->sliMaxSublayers = val + 1;
1565
0
  CHECK( sei->sliMaxSublayers > 6, "sli_max_sublayers_minus1 must be < 6 in vvdecSEISubpictureLevelInfo" );
1566
1567
0
  sei_read_flag(pDecodedMessageOutputStream,        val,    "sli_sublayer_info_present_flag");        sei->sliSublayerInfoPresentFlag = val;
1568
0
  while (!isByteAligned())
1569
0
  {
1570
0
    sei_read_flag( pDecodedMessageOutputStream,     val,    "sli_alignment_zero_bit" );           CHECK (val != 0, "sli_alignment_zero_bit not equal to zero" );
1571
0
  }
1572
1573
  // sei parameters initialization
1574
0
  for (int i = 0; i < sei->numRefLevels; i++)
1575
0
  {
1576
0
    for (int k = 0; k < sei->sliMaxSublayers; k++)
1577
0
    {
1578
0
      sei->refLevelIdc[i][k] = vvdecLevel::VVDEC_LEVEL15_5;
1579
0
    }
1580
0
  }
1581
0
  if (sei->explicitFractionPresentFlag)
1582
0
  {
1583
0
    for (int i = 0; i < sei->numRefLevels; i++)
1584
0
    {
1585
0
      for (int j = 0; j < sei->numSubpics; j++)
1586
0
      {
1587
0
        for (int k = 0; k < sei->sliMaxSublayers; k++)
1588
0
        {
1589
0
          sei->refLevelFraction[i][j][k] = 0;
1590
0
        }
1591
0
      }
1592
0
    }
1593
0
  }
1594
1595
  // parsing
1596
0
  for (int k = sei->sliSublayerInfoPresentFlag ? 0 : sei->sliMaxSublayers - 1; k < sei->sliMaxSublayers; k++)
1597
0
  {
1598
0
    for (int i = 0; i < sei->numRefLevels; i++)
1599
0
    {
1600
0
      sei_read_code(pDecodedMessageOutputStream, 8, val, "sli_non_subpic_layers_fraction[i][k]");    sei->nonSubpicLayersFraction[i][k] = (vvdecLevel) val;
1601
0
      sei_read_code(pDecodedMessageOutputStream, 8, val, "sli_ref_level_idc[i][k]");                 sei->refLevelIdc[i][k] = (vvdecLevel) val;
1602
1603
0
      if (sei->explicitFractionPresentFlag)
1604
0
      {
1605
0
        for (int j = 0; j < sei->numSubpics; j++)
1606
0
        {
1607
0
          sei_read_code(pDecodedMessageOutputStream, 8, val, "sli_ref_level_fraction_minus1[i][j][k]");  sei->refLevelFraction[i][j][k] = val;
1608
0
        }
1609
0
      }
1610
0
    }
1611
0
  }
1612
1613
  // update the inference of refLevelIdc[][] and refLevelFraction[][][]
1614
0
  if (!sei->sliSublayerInfoPresentFlag)
1615
0
  {
1616
0
    for (int k = sei->sliMaxSublayers - 2; k >= 0; k--)
1617
0
    {
1618
0
      for (int i = 0; i < sei->numRefLevels; i++)
1619
0
      {
1620
0
        sei->nonSubpicLayersFraction[i][k] = sei->nonSubpicLayersFraction[i][sei->sliMaxSublayers - 1];
1621
0
        sei->refLevelIdc            [i][k] = sei->refLevelIdc            [i][sei->sliMaxSublayers - 1];
1622
0
        if (sei->explicitFractionPresentFlag)
1623
0
        {
1624
0
          for (int j = 0; j < sei->numSubpics; j++)
1625
0
          {
1626
0
            sei->refLevelFraction[i][j][k] = sei->refLevelFraction[i][j][sei->sliMaxSublayers - 1];
1627
0
          }
1628
0
        }
1629
0
      }
1630
0
    }
1631
0
  }
1632
0
}
1633
1634
void SEIReader::xParseSEISampleAspectRatioInfo(vvdecSEI* s, uint32_t payloadSize, std::ostream *pDecodedMessageOutputStream)
1635
0
{
1636
0
  uint32_t val;
1637
0
  CHECK_FATAL( !s || s->payload == NULL, "allocation error in vvdecSEISampleAspectRatioInfo" );
1638
0
  output_sei_message_header(s, pDecodedMessageOutputStream, payloadSize);
1639
1640
0
  vvdecSEISampleAspectRatioInfo* sei =(vvdecSEISampleAspectRatioInfo*)s->payload;
1641
0
  ::memset(sei, 0, sizeof(vvdecSEISampleAspectRatioInfo));
1642
1643
0
  sei_read_flag( pDecodedMessageOutputStream,           val,    "sari_cancel_flag" );                      sei->sariCancelFlag = val;
1644
0
  if (!sei->sariCancelFlag)
1645
0
  {
1646
0
    sei_read_flag( pDecodedMessageOutputStream,         val,    "sari_persistence_flag" );                 sei->sariPersistenceFlag = val;
1647
0
    sei_read_code( pDecodedMessageOutputStream,     8,  val,    "sari_aspect_ratio_idc" );                 sei->sariAspectRatioIdc = val;
1648
0
    if (sei->sariAspectRatioIdc == 255)
1649
0
    {
1650
0
      sei_read_code( pDecodedMessageOutputStream,  16,  val,    "sari_sar_width" );                        sei->sariSarWidth = val;
1651
0
      sei_read_code( pDecodedMessageOutputStream,  16,  val,    "sari_sar_height" );                       sei->sariSarHeight = val;
1652
0
    }
1653
0
  }
1654
0
}
1655
1656
#if JVET_S0257_DUMP_360SEI_MESSAGE
1657
void SeiCfgFileDump::write360SeiDump (std::string decoded360MessageFileName, SEIMessages& seis, const SPS* sps)
1658
{
1659
  if (360SEIMessageDumped)
1660
  {
1661
    return;
1662
  }
1663
1664
  SEIMessages equirectangularProjectionSEIs = getSeisByType(seis, EQUIRECTANGULAR_PROJECTION);
1665
  if (!equirectangularProjectionSEIs.empty())
1666
  {
1667
    SEIEquirectangularProjection* sei = (SEIEquirectangularProjection*)equirectangularProjectionSEIs.front();
1668
    xDumpSEIEquirectangularProjection(*sei, sps, decoded360MessageFileName);
1669
    360SEIMessageDumped = true;
1670
  }
1671
  else
1672
  {
1673
    SEIMessages generalizedCubemapProjectionSEIs = getSeisByType(seis, GENERALIZED_CUBEMAP_PROJECTION);
1674
    if (!generalizedCubemapProjectionSEIs.empty())
1675
    {
1676
      SEIGeneralizedCubemapProjection* sei = (SEIGeneralizedCubemapProjection*)generalizedCubemapProjectionSEIs.front();
1677
      xDumpSEIGeneralizedCubemapProjection(*sei, sps, decoded360MessageFileName);
1678
      360SEIMessageDumped = true;
1679
    }
1680
  }
1681
}
1682
1683
void SeiCfgFileDump::xDumpSEIEquirectangularProjection     (SEIEquirectangularProjection &sei, const SPS* sps, std::string decoded360MessageFileName)
1684
{
1685
  if (!decoded360MessageFileName.empty())
1686
  {
1687
    FILE *fp = fopen(decoded360MessageFileName.c_str(), "w");
1688
    if (fp)
1689
    {
1690
      int chromaFormatTable[4] = {400, 420, 422, 444};
1691
      fprintf(fp, "InputBitDepth                 : %d    # Input bitdepth\n", sps->getBitDepth(CHANNEL_TYPE_LUMA));
1692
      fprintf(fp, "InputChromaFormat             : %d    # Ratio of luminance to chrominance samples\n", chromaFormatTable[sps->getChromaFormatIdc()]);
1693
      fprintf(fp, "SourceWidth                   : %d    # Input  frame width\n", sps->getMaxPicWidthInLumaSamples());
1694
      fprintf(fp, "SourceHeight                  : %d    # Input  frame height\n\n", sps->getMaxPicHeightInLumaSamples());
1695
1696
      fprintf(fp, "InputGeometryType             : 0     # 0: equirectangular; 1: cubemap; 2: equalarea; this should be in the cfg of per sequence.\n");
1697
      if (sei.erpGuardBandFlag == 1)
1698
      {
1699
        fprintf(fp, "InputPERP                     : 1     # 0: original ERP input; 1: padded ERP input\n");
1700
        fprintf(fp, "CodingPERP                    : 0     # 0: coding with original ERP size; 1: coding with padded ERP\n");
1701
      }
1702
      fclose(fp);
1703
      360SEIMessageDumped = true;
1704
    }
1705
    else
1706
    {
1707
      msg( ERROR, "File %s could not be opened.\n", decoded360MessageFileName.c_str() );
1708
    }
1709
  }
1710
}
1711
void SeiCfgFileDump::xDumpSEIGeneralizedCubemapProjection  (SEIGeneralizedCubemapProjection &sei, const SPS* sps, std::string decoded360MessageFileName)
1712
{
1713
  if (!sei.gcmpCancelFlag)
1714
  {
1715
    int numFace = sei.gcmpPackingType == 4 || sei.gcmpPackingType == 5 ? 5 : 6;
1716
    int packingTypeTable[6][2] = {{6, 1}, {3, 2}, {2, 3}, {1, 6}, {1, 5}, {5, 1}};
1717
    int rotationTable[4] = {0, 90, 180, 270};
1718
    std::string packingTypeStr = "";
1719
    std::string gcmpsettingsStr = "";
1720
    std::ostringstream oss;
1721
1722
    packingTypeStr += "SourceFPStructure                 : " + std::to_string(packingTypeTable[sei.gcmpPackingType][0]) + " " + std::to_string(packingTypeTable[sei.gcmpPackingType][1]);
1723
    gcmpsettingsStr += "InputGCMPSettings                 : ";
1724
1725
    for (int i = 0; i < numFace; i++)
1726
    {
1727
      int rotation = rotationTable[sei.gcmpFaceRotation[i]];
1728
      if (sei.gcmpFaceIndex[i] == 1)
1729
      {
1730
        rotation = (rotation + 270) % 360 + 360;
1731
      }
1732
      else if (sei.gcmpFaceIndex[i] == 2)
1733
      {
1734
        rotation = (rotation + 180) % 360 + 360;
1735
      }
1736
      else
1737
      {
1738
        rotation += 360;
1739
      }
1740
      if (i % packingTypeTable[sei.gcmpPackingType][1] == 0)
1741
      {
1742
        packingTypeStr += "   ";
1743
      }
1744
      packingTypeStr += std::to_string(sei.gcmpFaceIndex[i]) + " " + std::to_string(rotation) + " ";
1745
1746
      if (sei.gcmpMappingFunctionType == 2)
1747
      {
1748
        double a = ((int)sei.gcmpFunctionCoeffU[i] + 1) / 128.0;
1749
        double b = ((int)sei.gcmpFunctionCoeffV[i] + 1) / 128.0;
1750
        oss.str("");
1751
        oss<<a;
1752
        std::string a_str = oss.str();
1753
        oss.str("");
1754
        oss<<b;
1755
        std::string b_str = oss.str();
1756
        gcmpsettingsStr += a_str + " " + std::to_string(sei.gcmpFunctionUAffectedByVFlag[i]) + " " + b_str + " " + std::to_string(sei.gcmpFunctionVAffectedByUFlag[i]) + "   ";
1757
      }
1758
    }
1759
    if (!decoded360MessageFileName.empty())
1760
    {
1761
      FILE *fp = fopen(decoded360MessageFileName.c_str(), "w");
1762
      if (fp)
1763
      {
1764
        int chromaFormatTable[4] = {400, 420, 422, 444};
1765
        fprintf(fp, "InputBitDepth                 : %d    # Input bitdepth\n", sps->getBitDepth(CHANNEL_TYPE_LUMA));
1766
        fprintf(fp, "InputChromaFormat             : %d    # Ratio of luminance to chrominance samples\n", chromaFormatTable[sps->getChromaFormatIdc()]);
1767
        fprintf(fp, "SourceWidth                   : %d    # Input  frame width\n", sps->getMaxPicWidthInLumaSamples());
1768
        fprintf(fp, "SourceHeight                  : %d    # Input  frame height\n\n", sps->getMaxPicHeightInLumaSamples());
1769
1770
        fprintf(fp, "InputGeometryType             : 15    # 0: equirectangular; 1: cubemap; 2: equalarea; this should be in the cfg of per sequence.\n");
1771
1772
        packingTypeStr += " # frame packing order: numRows numCols Row0Idx0 ROT Row0Idx1 ROT ... Row1...";
1773
        gcmpsettingsStr += " # mapping function parameters for each face: u coefficient, u affected by v flag, v coefficient, v affected by u flag";
1774
        fprintf(fp, "%s\n", packingTypeStr.c_str());
1775
        fprintf(fp, "InputGCMPMappingType              : %d                                    # 0: CMP; 1: EAC; 2: parameterized CMP\n", (int)sei.gcmpMappingFunctionType);
1776
        if ((int)sei.gcmpMappingFunctionType == 2)
1777
          fprintf(fp, "%s\n", gcmpsettingsStr.c_str());
1778
        fprintf(fp, "InputGCMPPaddingFlag              : %d                                   # 0: input without guard bands; 1: input with guard bands\n", sei.gcmpGuardBandFlag);
1779
        if (sei.gcmpGuardBandFlag)
1780
        {
1781
          fprintf(fp, "InputGCMPPaddingType              : %d                                   # 0: unspecified(repetitive padding is used); 1: repetitive padding; 2: copy from neighboring face; 3: geometry padding\n", (int)sei.gcmpGuardBandType);
1782
          fprintf(fp, "InputGCMPPaddingExteriorFlag      : %d                                   # 0: guard bands only on discontinuous edges; 1: guard bands on both discontinuous edges and frame boundaries\n", sei.gcmpGuardBandBoundaryExteriorFlag);
1783
          fprintf(fp, "InputGCMPPaddingSize              : %d                                   # guard band size for input GCMP\n", (int)sei.gcmpGuardBandSamplesMinus1 + 1);
1784
        }
1785
        fclose(fp);
1786
        360SEIMessageDumped = true;
1787
      }
1788
      else
1789
      {
1790
        msg( ERROR, "File %s could not be opened.\n", decoded360MessageFileName.c_str() );
1791
      }
1792
    }
1793
  }
1794
}
1795
1796
#endif
1797
1798
}