Coverage Report

Created: 2022-08-24 06:17

/src/libde265/libde265/sei.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "sei.h"
22
#include "util.h"
23
#include "md5.h"
24
25
#include "libde265/sps.h"
26
#include "libde265/image.h"
27
#include "libde265/decctx.h"
28
29
#include <assert.h>
30
31
32
static de265_error read_sei_decoded_picture_hash(bitreader* reader, sei_message* sei,
33
                                                 const seq_parameter_set* sps)
34
0
{
35
0
  sei_decoded_picture_hash* seihash = &sei->data.decoded_picture_hash;
36
37
0
  seihash->hash_type = (enum sei_decoded_picture_hash_type)get_bits(reader,8);
38
39
0
  if (sps==NULL) {
40
0
    return DE265_WARNING_SPS_MISSING_CANNOT_DECODE_SEI;
41
0
  }
42
43
0
  int nHashes = sps->chroma_format_idc==0 ? 1 : 3;
44
0
  for (int i=0;i<nHashes;i++) {
45
0
    switch (seihash->hash_type) {
46
0
    case sei_decoded_picture_hash_type_MD5:
47
0
      for (int b=0;b<16;b++) { seihash->md5[i][b] = get_bits(reader,8); }
48
0
      break;
49
50
0
    case sei_decoded_picture_hash_type_CRC:
51
0
      seihash->crc[i] = get_bits(reader,16);
52
0
      break;
53
54
0
    case sei_decoded_picture_hash_type_checksum:
55
0
      seihash->checksum[i]  = get_bits(reader,32);
56
0
      break;
57
0
    }
58
0
  }
59
60
0
  return DE265_OK;
61
0
}
62
63
64
static void dump_sei_decoded_picture_hash(const sei_message* sei,
65
                                          const seq_parameter_set* sps)
66
0
{
67
0
  const sei_decoded_picture_hash* seihash = &sei->data.decoded_picture_hash;
68
69
0
  loginfo(LogSEI,"  hash_type: ");
70
0
  switch (seihash->hash_type) {
71
0
  case sei_decoded_picture_hash_type_MD5: loginfo(LogSEI,"MD5\n"); break;
72
0
  case sei_decoded_picture_hash_type_CRC: loginfo(LogSEI,"CRC\n"); break;
73
0
  case sei_decoded_picture_hash_type_checksum: loginfo(LogSEI,"checksum\n"); break;
74
0
  }
75
76
0
  int nHashes = sps->chroma_format_idc==0 ? 1 : 3;
77
0
  for (int i=0;i<nHashes;i++) {
78
0
    switch (seihash->hash_type) {
79
0
    case sei_decoded_picture_hash_type_MD5:
80
0
      loginfo(LogSEI,"  MD5[%d]: %02x", i,seihash->md5[i][0]);
81
0
      for (int b=1;b<16;b++) {
82
0
        loginfo(LogSEI,"*:%02x", seihash->md5[i][b]);
83
0
      }
84
0
      loginfo(LogSEI,"*\n");
85
0
      break;
86
87
0
    case sei_decoded_picture_hash_type_CRC:
88
0
      loginfo(LogSEI,"  CRC[%d]: %02x\n", i,seihash->crc[i]);
89
0
      break;
90
91
0
    case sei_decoded_picture_hash_type_checksum:
92
0
      loginfo(LogSEI,"  checksum[%d]: %04x\n", i,seihash->checksum[i]);
93
0
      break;
94
0
    }
95
0
  }
96
0
}
97
98
99
class raw_hash_data
100
{
101
public:
102
  raw_hash_data(int w, int stride);
103
  ~raw_hash_data();
104
105
  struct data_chunk {
106
    const uint8_t* data;
107
    int            len;
108
  };
109
110
  data_chunk prepare_8bit(const uint8_t* data,int y);
111
  data_chunk prepare_16bit(const uint8_t* data,int y);
112
113
private:
114
  int mWidth, mStride;
115
116
  uint8_t* mMem;
117
};
118
119
120
raw_hash_data::raw_hash_data(int w, int stride)
121
0
{
122
0
  mWidth=w;
123
0
  mStride=stride;
124
0
  mMem = NULL;
125
0
}
126
127
raw_hash_data::~raw_hash_data()
128
0
{
129
0
  delete[] mMem;
130
0
}
131
132
raw_hash_data::data_chunk raw_hash_data::prepare_8bit(const uint8_t* data,int y)
133
0
{
134
0
  data_chunk chunk;
135
0
  chunk.data = data+y*mStride;
136
0
  chunk.len  = mWidth;
137
0
  return chunk;
138
0
}
139
140
raw_hash_data::data_chunk raw_hash_data::prepare_16bit(const uint8_t* data,int y)
141
0
{
142
0
  if (mMem == NULL) {
143
0
    mMem = new uint8_t[2*mWidth];
144
0
  }
145
146
0
  const uint16_t* data16 = (uint16_t*)data;
147
148
0
  for (int x=0; x<mWidth; x++) {
149
0
    mMem[2*x+0] = data16[y*mStride+x] & 0xFF;
150
0
    mMem[2*x+1] = data16[y*mStride+x] >> 8;
151
0
  }
152
153
0
  data_chunk chunk;
154
0
  chunk.data = mMem;
155
0
  chunk.len  = 2*mWidth;
156
0
  return chunk;
157
0
}
158
159
160
static uint32_t compute_checksum_8bit(uint8_t* data,int w,int h,int stride, int bit_depth)
161
0
{
162
0
  uint32_t sum = 0;
163
164
0
  if (bit_depth<=8) {
165
0
    for (int y=0; y<h; y++)
166
0
      for(int x=0; x<w; x++) {
167
0
        uint8_t xorMask = ( x & 0xFF ) ^ ( y & 0xFF ) ^ ( x  >>  8 ) ^ ( y  >>  8 );
168
0
        sum += data[y*stride + x] ^ xorMask;
169
0
      }
170
0
  }
171
0
  else {
172
0
    for (int y=0; y<h; y++)
173
0
      for(int x=0; x<w; x++) {
174
0
        uint8_t xorMask = ( x & 0xFF ) ^ ( y & 0xFF ) ^ ( x  >>  8 ) ^ ( y  >>  8 );
175
0
        sum += (data[y*stride + x] & 0xFF) ^ xorMask;
176
0
        sum += (data[y*stride + x] >> 8)   ^ xorMask;
177
0
      }
178
0
  }
179
180
0
  return sum & 0xFFFFFFFF;
181
0
}
182
183
static inline uint16_t crc_process_byte(uint16_t crc, uint8_t byte)
184
0
{
185
0
  for (int bit=0;bit<8;bit++) {
186
0
    int bitVal = (byte >> (7-bit)) & 1;
187
0
188
0
    int crcMsb = (crc>>15) & 1;
189
0
    crc = (((crc<<1) + bitVal) & 0xFFFF);
190
0
191
0
    if (crcMsb) { crc ^=  0x1021; }
192
0
  }
193
0
194
0
  return crc;
195
0
}
196
197
/*
198
static uint16_t compute_CRC_8bit_old(const uint8_t* data,int w,int h,int stride)
199
{
200
  uint16_t crc = 0xFFFF;
201
202
  for (int y=0; y<h; y++)
203
    for(int x=0; x<w; x++) {
204
      crc = crc_process_byte(crc, data[y*stride+x]);
205
    }
206
207
  crc = crc_process_byte(crc, 0);
208
  crc = crc_process_byte(crc, 0);
209
210
  return crc;
211
}
212
*/
213
214
static inline uint16_t crc_process_byte_parallel(uint16_t crc, uint8_t byte)
215
0
{
216
0
  uint16_t s = byte ^ (crc >> 8);
217
0
  uint16_t t = s ^ (s >> 4);
218
219
0
  return  ((crc << 8) ^
220
0
     t ^
221
0
     (t <<  5) ^
222
0
     (t << 12)) & 0xFFFF;
223
0
}
224
225
static uint32_t compute_CRC_8bit_fast(const uint8_t* data,int w,int h,int stride, int bit_depth)
226
0
{
227
0
  raw_hash_data raw_data(w,stride);
228
229
0
  uint16_t crc = 0xFFFF;
230
231
0
  crc = crc_process_byte_parallel(crc, 0);
232
0
  crc = crc_process_byte_parallel(crc, 0);
233
234
0
  for (int y=0; y<h; y++) {
235
0
    raw_hash_data::data_chunk chunk;
236
237
0
    if (bit_depth>8)
238
0
      chunk = raw_data.prepare_16bit(data, y);
239
0
    else
240
0
      chunk = raw_data.prepare_8bit(data, y);
241
242
0
    for(int x=0; x<chunk.len; x++) {
243
0
      crc = crc_process_byte_parallel(crc, chunk.data[x]);
244
0
    }
245
0
  }
246
247
0
  return crc;
248
0
}
249
250
251
static void compute_MD5(uint8_t* data,int w,int h,int stride, uint8_t* result, int bit_depth)
252
0
{
253
0
  MD5_CTX md5;
254
0
  MD5_Init(&md5);
255
256
0
  raw_hash_data raw_data(w,stride);
257
258
0
  for (int y=0; y<h; y++) {
259
0
    raw_hash_data::data_chunk chunk;
260
261
0
    if (bit_depth>8)
262
0
      chunk = raw_data.prepare_16bit(data, y);
263
0
    else
264
0
      chunk = raw_data.prepare_8bit(data, y);
265
266
0
    MD5_Update(&md5, (void*)chunk.data, chunk.len);
267
0
  }
268
269
0
  MD5_Final(result, &md5);
270
0
}
271
272
273
static de265_error process_sei_decoded_picture_hash(const sei_message* sei, de265_image* img)
274
0
{
275
0
  const sei_decoded_picture_hash* seihash = &sei->data.decoded_picture_hash;
276
277
  /* Do not check SEI on pictures that are not output.
278
     Hash may be wrong, because of a broken link (BLA).
279
     This happens, for example in conformance stream RAP_B, where a EOS-NAL
280
     appears before a CRA (POC=32). */
281
0
  if (img->PicOutputFlag == false) {
282
0
    return DE265_OK;
283
0
  }
284
285
  //write_picture(img);
286
287
0
  int nHashes = img->get_sps().chroma_format_idc==0 ? 1 : 3;
288
0
  for (int i=0;i<nHashes;i++) {
289
0
    uint8_t* data;
290
0
    int w,h,stride;
291
292
0
    w = img->get_width(i);
293
0
    h = img->get_height(i);
294
295
0
    data = img->get_image_plane(i);
296
0
    stride = img->get_image_stride(i);
297
298
0
    switch (seihash->hash_type) {
299
0
    case sei_decoded_picture_hash_type_MD5:
300
0
      {
301
0
        uint8_t md5[16];
302
0
        compute_MD5(data,w,h,stride,md5, img->get_bit_depth(i));
303
304
/*
305
        fprintf(stderr,"computed MD5: ");
306
        for (int b=0;b<16;b++) {
307
          fprintf(stderr,"%02x", md5[b]);
308
        }
309
        fprintf(stderr,"\n");
310
*/
311
312
0
        for (int b=0;b<16;b++) {
313
0
          if (md5[b] != seihash->md5[i][b]) {
314
/*
315
            fprintf(stderr,"SEI decoded picture MD5 mismatch (POC=%d)\n", img->PicOrderCntVal);
316
*/
317
0
            return DE265_ERROR_CHECKSUM_MISMATCH;
318
0
          }
319
0
        }
320
0
      }
321
0
      break;
322
323
0
    case sei_decoded_picture_hash_type_CRC:
324
0
      {
325
0
        uint16_t crc = compute_CRC_8bit_fast(data,w,h,stride, img->get_bit_depth(i));
326
327
0
        logtrace(LogSEI,"SEI decoded picture hash: %04x <-[%d]-> decoded picture: %04x\n",
328
0
                 seihash->crc[i], i, crc);
329
330
0
        if (crc != seihash->crc[i]) {
331
/*
332
          fprintf(stderr,"SEI decoded picture hash: %04x, decoded picture: %04x (POC=%d)\n",
333
                  seihash->crc[i], crc, img->PicOrderCntVal);
334
*/
335
0
          return DE265_ERROR_CHECKSUM_MISMATCH;
336
0
        }
337
0
      }
338
0
      break;
339
340
0
    case sei_decoded_picture_hash_type_checksum:
341
0
      {
342
0
        uint32_t chksum = compute_checksum_8bit(data,w,h,stride, img->get_bit_depth(i));
343
344
0
        if (chksum != seihash->checksum[i]) {
345
/*
346
          fprintf(stderr,"SEI decoded picture hash: %04x, decoded picture: %04x (POC=%d)\n",
347
                  seihash->checksum[i], chksum, img->PicOrderCntVal);
348
*/
349
0
          return DE265_ERROR_CHECKSUM_MISMATCH;
350
0
        }
351
0
      }
352
0
      break;
353
0
    }
354
0
  }
355
356
0
  loginfo(LogSEI,"decoded picture hash checked: OK\n");
357
  //printf("checked picture %d SEI: OK\n", img->PicOrderCntVal);
358
359
0
  return DE265_OK;
360
0
}
361
362
363
de265_error read_sei(bitreader* reader, sei_message* sei, bool suffix, const seq_parameter_set* sps)
364
50
{
365
50
  int payload_type = 0;
366
50
  for (;;)
367
50
    {
368
50
      int byte = get_bits(reader,8);
369
50
      payload_type += byte;
370
50
      if (byte != 0xFF) { break; }
371
50
    }
372
373
  //printf("SEI payload: %d\n",payload_type);
374
375
50
  int payload_size = 0;
376
50
  for (;;)
377
50
    {
378
50
      int byte = get_bits(reader,8);
379
50
      payload_size += byte;
380
50
      if (byte != 0xFF) { break; }
381
50
    }
382
383
50
  sei->payload_type = (enum sei_payload_type)payload_type;
384
50
  sei->payload_size = payload_size;
385
386
387
  // --- sei message dispatch
388
389
50
  de265_error err = DE265_OK;
390
391
50
  switch (sei->payload_type) {
392
0
  case sei_payload_type_decoded_picture_hash:
393
0
    err = read_sei_decoded_picture_hash(reader,sei,sps);
394
0
    break;
395
396
50
  default:
397
    // TODO: unknown SEI messages are ignored
398
50
    break;
399
50
  }
400
401
50
  return err;
402
50
}
403
404
void dump_sei(const sei_message* sei, const seq_parameter_set* sps)
405
50
{
406
50
  loginfo(LogHeaders,"SEI message: %s\n", sei_type_name(sei->payload_type));
407
408
50
  switch (sei->payload_type) {
409
0
  case sei_payload_type_decoded_picture_hash:
410
0
    dump_sei_decoded_picture_hash(sei, sps);
411
0
    break;
412
413
50
  default:
414
    // TODO: unknown SEI messages are ignored
415
50
    break;
416
50
  }
417
50
}
418
419
420
de265_error process_sei(const sei_message* sei, de265_image* img)
421
0
{
422
0
  de265_error err = DE265_OK;
423
424
0
  switch (sei->payload_type) {
425
0
  case sei_payload_type_decoded_picture_hash:
426
0
    if (img->decctx->param_sei_check_hash) {
427
0
      err = process_sei_decoded_picture_hash(sei, img);
428
0
      if (err==DE265_OK) {
429
        //printf("SEI check ok\n");
430
0
      }
431
0
    }
432
433
0
    break;
434
435
0
  default:
436
    // TODO: unknown SEI messages are ignored
437
0
    break;
438
0
  }
439
440
0
  return err;
441
0
}
442
443
444
const char* sei_type_name(enum sei_payload_type type)
445
0
{
446
0
  switch (type) {
447
0
  case sei_payload_type_buffering_period:
448
0
    return "buffering_period";
449
0
  case sei_payload_type_pic_timing:
450
0
    return "pic_timing";
451
0
  case sei_payload_type_pan_scan_rect:
452
0
    return "pan_scan_rect";
453
0
  case sei_payload_type_filler_payload:
454
0
    return "filler_payload";
455
0
  case sei_payload_type_user_data_registered_itu_t_t35:
456
0
    return "user_data_registered_itu_t_t35";
457
0
  case sei_payload_type_user_data_unregistered:
458
0
    return "user_data_unregistered";
459
0
  case sei_payload_type_recovery_point:
460
0
    return "recovery_point";
461
0
  case sei_payload_type_scene_info:
462
0
    return "scene_info";
463
0
  case sei_payload_type_picture_snapshot:
464
0
    return "picture_snapshot";
465
0
  case sei_payload_type_progressive_refinement_segment_start:
466
0
    return "progressive_refinement_segment_start";
467
0
  case sei_payload_type_progressive_refinement_segment_end:
468
0
    return "progressive_refinement_segment_end";
469
0
  case sei_payload_type_film_grain_characteristics:
470
0
    return "film_grain_characteristics";
471
0
  case sei_payload_type_post_filter_hint:
472
0
    return "post_filter_hint";
473
0
  case sei_payload_type_tone_mapping_info:
474
0
    return "tone_mapping_info";
475
0
  case sei_payload_type_frame_packing_arrangement:
476
0
    return "frame_packing_arrangement";
477
0
  case sei_payload_type_display_orientation:
478
0
    return "display_orientation";
479
0
  case sei_payload_type_structure_of_pictures_info:
480
0
    return "structure_of_pictures_info";
481
0
  case sei_payload_type_active_parameter_sets:
482
0
    return "active_parameter_sets";
483
0
  case sei_payload_type_decoding_unit_info:
484
0
    return "decoding_unit_info";
485
0
  case sei_payload_type_temporal_sub_layer_zero_index:
486
0
    return "temporal_sub_layer_zero_index";
487
0
  case sei_payload_type_decoded_picture_hash:
488
0
    return "decoded_picture_hash";
489
0
  case sei_payload_type_scalable_nesting:
490
0
    return "scalable_nesting";
491
0
  case sei_payload_type_region_refresh_info:
492
0
    return "region_refresh_info";
493
0
  case sei_payload_type_no_display:
494
0
    return "no_display";
495
0
  case sei_payload_type_motion_constrained_tile_sets:
496
0
    return "motion_constrained_tile_sets";
497
498
0
  default:
499
0
    return "unknown SEI message";
500
0
  }
501
0
}