FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_sei.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 179 315 56.8%
Branches: 77 155 49.7%

Line Branch Exec Source
1 /*
2 * HEVC Supplementary Enhancement Information messages
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Gildas Cocherel
6 * Copyright (C) 2013 Vittorio Giovara
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "atsc_a53.h"
26 #include "bytestream.h"
27 #include "dynamic_hdr10_plus.h"
28 #include "dynamic_hdr_vivid.h"
29 #include "golomb.h"
30 #include "hevc_ps.h"
31 #include "hevc_sei.h"
32
33 6852 static int decode_nal_sei_decoded_picture_hash(HEVCSEIPictureHash *s,
34 GetByteContext *gb)
35 {
36 int cIdx;
37 uint8_t hash_type;
38 //uint16_t picture_crc;
39 //uint32_t picture_checksum;
40 6852 hash_type = bytestream2_get_byte(gb);
41
42
2/2
✓ Branch 0 taken 20556 times.
✓ Branch 1 taken 6852 times.
27408 for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
43
2/2
✓ Branch 0 taken 19509 times.
✓ Branch 1 taken 1047 times.
20556 if (hash_type == 0) {
44 19509 s->is_md5 = 1;
45 19509 bytestream2_get_buffer(gb, s->md5[cIdx], sizeof(s->md5[cIdx]));
46 } else if (hash_type == 1) {
47 // picture_crc = get_bits(gb, 16);
48 } else if (hash_type == 2) {
49 // picture_checksum = get_bits_long(gb, 32);
50 }
51 }
52 6852 return 0;
53 }
54
55 6 static int decode_nal_sei_mastering_display_info(HEVCSEIMasteringDisplay *s,
56 GetByteContext *gb)
57 {
58 int i;
59
60
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_bytes_left(gb) < 24)
61 return AVERROR_INVALIDDATA;
62
63 // Mastering primaries
64
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 for (i = 0; i < 3; i++) {
65 18 s->display_primaries[i][0] = bytestream2_get_be16u(gb);
66 18 s->display_primaries[i][1] = bytestream2_get_be16u(gb);
67 }
68 // White point (x, y)
69 6 s->white_point[0] = bytestream2_get_be16u(gb);
70 6 s->white_point[1] = bytestream2_get_be16u(gb);
71
72 // Max and min luminance of mastering display
73 6 s->max_luminance = bytestream2_get_be32u(gb);
74 6 s->min_luminance = bytestream2_get_be32u(gb);
75
76 // As this SEI message comes before the first frame that references it,
77 // initialize the flag to 2 and decrement on IRAP access unit so it
78 // persists for the coded video sequence (e.g., between two IRAPs)
79 6 s->present = 2;
80
81 6 return 0;
82 }
83
84 6 static int decode_nal_sei_content_light_info(HEVCSEIContentLight *s,
85 GetByteContext *gb)
86 {
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_bytes_left(gb) < 4)
88 return AVERROR_INVALIDDATA;
89
90 // Max and average light levels
91 6 s->max_content_light_level = bytestream2_get_be16u(gb);
92 6 s->max_pic_average_light_level = bytestream2_get_be16u(gb);
93 // As this SEI message comes before the first frame that references it,
94 // initialize the flag to 2 and decrement on IRAP access unit so it
95 // persists for the coded video sequence (e.g., between two IRAPs)
96 6 s->present = 2;
97
98 6 return 0;
99 }
100
101 static int decode_nal_sei_frame_packing_arrangement(HEVCSEIFramePacking *s, GetBitContext *gb)
102 {
103 get_ue_golomb_long(gb); // frame_packing_arrangement_id
104 s->present = !get_bits1(gb);
105
106 if (s->present) {
107 s->arrangement_type = get_bits(gb, 7);
108 s->quincunx_subsampling = get_bits1(gb);
109 s->content_interpretation_type = get_bits(gb, 6);
110
111 // spatial_flipping_flag, frame0_flipped_flag, field_views_flag
112 skip_bits(gb, 3);
113 s->current_frame_is_frame0_flag = get_bits1(gb);
114 }
115 return 0;
116 }
117
118 static int decode_nal_sei_display_orientation(HEVCSEIDisplayOrientation *s, GetBitContext *gb)
119 {
120 s->present = !get_bits1(gb);
121
122 if (s->present) {
123 s->hflip = get_bits1(gb); // hor_flip
124 s->vflip = get_bits1(gb); // ver_flip
125
126 s->anticlockwise_rotation = get_bits(gb, 16);
127 // skip_bits1(gb); // display_orientation_persistence_flag
128 }
129
130 return 0;
131 }
132
133 1204 static int decode_nal_sei_pic_timing(HEVCSEI *s, GetBitContext *gb,
134 const HEVCParamSets *ps, void *logctx)
135 {
136 1204 HEVCSEIPictureTiming *h = &s->picture_timing;
137 HEVCSPS *sps;
138
139
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 1160 times.
1204 if (!ps->sps_list[s->active_seq_parameter_set_id])
140 44 return(AVERROR(ENOMEM));
141 1160 sps = (HEVCSPS*)ps->sps_list[s->active_seq_parameter_set_id]->data;
142
143
2/2
✓ Branch 0 taken 677 times.
✓ Branch 1 taken 483 times.
1160 if (sps->vui.frame_field_info_present_flag) {
144 677 int pic_struct = get_bits(gb, 4);
145 677 h->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
146
4/6
✓ Branch 0 taken 677 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 667 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 667 times.
677 if (pic_struct == 2 || pic_struct == 10 || pic_struct == 12) {
147 10 av_log(logctx, AV_LOG_DEBUG, "BOTTOM Field\n");
148 10 h->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
149
4/6
✓ Branch 0 taken 667 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 667 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 656 times.
667 } else if (pic_struct == 1 || pic_struct == 9 || pic_struct == 11) {
150 11 av_log(logctx, AV_LOG_DEBUG, "TOP Field\n");
151 11 h->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 } else if (pic_struct == 7) {
153 av_log(logctx, AV_LOG_DEBUG, "Frame/Field Doubling\n");
154 h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING;
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 } else if (pic_struct == 8) {
156 av_log(logctx, AV_LOG_DEBUG, "Frame/Field Tripling\n");
157 h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING;
158 }
159 }
160
161 1160 return 0;
162 }
163
164 static int decode_registered_user_data_closed_caption(HEVCSEIA53Caption *s,
165 GetByteContext *gb)
166 {
167 int ret;
168
169 ret = ff_parse_a53_cc(&s->buf_ref, gb->buffer,
170 bytestream2_get_bytes_left(gb));
171 if (ret < 0)
172 return ret;
173
174 return 0;
175 }
176
177 13 static int decode_nal_sei_user_data_unregistered(HEVCSEIUnregistered *s,
178 GetByteContext *gb)
179 {
180 AVBufferRef *buf_ref, **tmp;
181 13 int size = bytestream2_get_bytes_left(gb);
182
183
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (size < 16 || size >= INT_MAX - 1)
184 return AVERROR_INVALIDDATA;
185
186 13 tmp = av_realloc_array(s->buf_ref, s->nb_buf_ref + 1, sizeof(*s->buf_ref));
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!tmp)
188 return AVERROR(ENOMEM);
189 13 s->buf_ref = tmp;
190
191 13 buf_ref = av_buffer_alloc(size + 1);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!buf_ref)
193 return AVERROR(ENOMEM);
194
195 13 bytestream2_get_bufferu(gb, buf_ref->data, size);
196 13 buf_ref->data[size] = 0;
197 13 buf_ref->size = size;
198 13 s->buf_ref[s->nb_buf_ref++] = buf_ref;
199
200 13 return 0;
201 }
202
203 3 static int decode_registered_user_data_dynamic_hdr_plus(HEVCSEIDynamicHDRPlus *s,
204 GetByteContext *gb)
205 {
206 size_t meta_size;
207 int err;
208 3 AVDynamicHDRPlus *metadata = av_dynamic_hdr_plus_alloc(&meta_size);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!metadata)
210 return AVERROR(ENOMEM);
211
212 3 err = ff_parse_itu_t_t35_to_dynamic_hdr10_plus(metadata, gb->buffer,
213 bytestream2_get_bytes_left(gb));
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0) {
215 av_free(metadata);
216 return err;
217 }
218
219 3 av_buffer_unref(&s->info);
220 3 s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->info) {
222 av_free(metadata);
223 return AVERROR(ENOMEM);
224 }
225
226 3 return 0;
227 }
228
229 3 static int decode_registered_user_data_dynamic_hdr_vivid(HEVCSEIDynamicHDRVivid *s,
230 GetByteContext *gb)
231 {
232 size_t meta_size;
233 int err;
234 3 AVDynamicHDRVivid *metadata = av_dynamic_hdr_vivid_alloc(&meta_size);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!metadata)
236 return AVERROR(ENOMEM);
237
238 3 err = ff_parse_itu_t_t35_to_dynamic_hdr_vivid(metadata,
239 gb->buffer, bytestream2_get_bytes_left(gb));
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0) {
241 av_free(metadata);
242 return err;
243 }
244
245 3 av_buffer_unref(&s->info);
246 3 s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0);
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->info) {
248 av_free(metadata);
249 return AVERROR(ENOMEM);
250 }
251
252 3 return 0;
253 }
254
255 70 static int decode_nal_sei_user_data_registered_itu_t_t35(HEVCSEI *s, GetByteContext *gb,
256 void *logctx)
257 {
258 int country_code, provider_code;
259
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
70 if (bytestream2_get_bytes_left(gb) < 3)
261 return AVERROR_INVALIDDATA;
262
263 70 country_code = bytestream2_get_byteu(gb);
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (country_code == 0xFF) {
265 if (bytestream2_get_bytes_left(gb) < 3)
266 return AVERROR_INVALIDDATA;
267
268 bytestream2_skipu(gb, 1);
269 }
270
271
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
70 if (country_code != 0xB5 && country_code != 0x26) { // usa_country_code and cn_country_code
272 av_log(logctx, AV_LOG_VERBOSE,
273 "Unsupported User Data Registered ITU-T T35 SEI message (country_code = 0x%x)\n",
274 country_code);
275 return 0;
276 }
277
278 70 provider_code = bytestream2_get_be16u(gb);
279
280
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
70 switch (provider_code) {
281 3 case 0x04: { // cuva_provider_code
282 3 const uint16_t cuva_provider_oriented_code = 0x0005;
283 uint16_t provider_oriented_code;
284
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(gb) < 2)
286 return AVERROR_INVALIDDATA;
287
288 3 provider_oriented_code = bytestream2_get_be16u(gb);
289
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (provider_oriented_code == cuva_provider_oriented_code) {
290 3 return decode_registered_user_data_dynamic_hdr_vivid(&s->dynamic_hdr_vivid, gb);
291 }
292 break;
293 }
294 3 case 0x3C: { // smpte_provider_code
295 // A/341 Amendment - 2094-40
296 3 const uint16_t smpte2094_40_provider_oriented_code = 0x0001;
297 3 const uint8_t smpte2094_40_application_identifier = 0x04;
298 uint16_t provider_oriented_code;
299 uint8_t application_identifier;
300
301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(gb) < 3)
302 return AVERROR_INVALIDDATA;
303
304 3 provider_oriented_code = bytestream2_get_be16u(gb);
305 3 application_identifier = bytestream2_get_byteu(gb);
306
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (provider_oriented_code == smpte2094_40_provider_oriented_code &&
307 application_identifier == smpte2094_40_application_identifier) {
308 3 return decode_registered_user_data_dynamic_hdr_plus(&s->dynamic_hdr_plus, gb);
309 }
310 break;
311 }
312 64 case 0x31: { // atsc_provider_code
313 uint32_t user_identifier;
314
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
64 if (bytestream2_get_bytes_left(gb) < 4)
316 return AVERROR_INVALIDDATA;
317
318 64 user_identifier = bytestream2_get_be32u(gb);
319 switch (user_identifier) {
320 case MKBETAG('G', 'A', '9', '4'):
321 return decode_registered_user_data_closed_caption(&s->a53_caption, gb);
322 64 default:
323 64 av_log(logctx, AV_LOG_VERBOSE,
324 "Unsupported User Data Registered ITU-T T35 SEI message (atsc user_identifier = 0x%04x)\n",
325 user_identifier);
326 64 break;
327 }
328 64 break;
329 }
330 default:
331 av_log(logctx, AV_LOG_VERBOSE,
332 "Unsupported User Data Registered ITU-T T35 SEI message (provider_code = %d)\n",
333 provider_code);
334 break;
335 }
336
337 64 return 0;
338 }
339
340 30 static int decode_nal_sei_active_parameter_sets(HEVCSEI *s, GetBitContext *gb, void *logctx)
341 {
342 int num_sps_ids_minus1;
343 unsigned active_seq_parameter_set_id;
344
345 30 get_bits(gb, 4); // active_video_parameter_set_id
346 30 get_bits(gb, 1); // self_contained_cvs_flag
347 30 get_bits(gb, 1); // num_sps_ids_minus1
348 30 num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
349
350
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
351 av_log(logctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
352 return AVERROR_INVALIDDATA;
353 }
354
355 30 active_seq_parameter_set_id = get_ue_golomb_long(gb);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) {
357 av_log(logctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
358 return AVERROR_INVALIDDATA;
359 }
360 30 s->active_seq_parameter_set_id = active_seq_parameter_set_id;
361
362 30 return 0;
363 }
364
365 static int decode_nal_sei_alternative_transfer(HEVCSEIAlternativeTransfer *s,
366 GetByteContext *gb)
367 {
368 if (bytestream2_get_bytes_left(gb) < 1)
369 return AVERROR_INVALIDDATA;
370
371 s->present = 1;
372 s->preferred_transfer_characteristics = bytestream2_get_byteu(gb);
373
374 return 0;
375 }
376
377 64 static int decode_nal_sei_timecode(HEVCSEITimeCode *s, GetBitContext *gb)
378 {
379 64 s->num_clock_ts = get_bits(gb, 2);
380
381
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
128 for (int i = 0; i < s->num_clock_ts; i++) {
382 64 s->clock_timestamp_flag[i] = get_bits(gb, 1);
383
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (s->clock_timestamp_flag[i]) {
385 s->units_field_based_flag[i] = get_bits(gb, 1);
386 s->counting_type[i] = get_bits(gb, 5);
387 s->full_timestamp_flag[i] = get_bits(gb, 1);
388 s->discontinuity_flag[i] = get_bits(gb, 1);
389 s->cnt_dropped_flag[i] = get_bits(gb, 1);
390
391 s->n_frames[i] = get_bits(gb, 9);
392
393 if (s->full_timestamp_flag[i]) {
394 s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
395 s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
396 s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
397 } else {
398 s->seconds_flag[i] = get_bits(gb, 1);
399 if (s->seconds_flag[i]) {
400 s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
401 s->minutes_flag[i] = get_bits(gb, 1);
402 if (s->minutes_flag[i]) {
403 s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
404 s->hours_flag[i] = get_bits(gb, 1);
405 if (s->hours_flag[i]) {
406 s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
407 }
408 }
409 }
410 }
411
412 s->time_offset_length[i] = get_bits(gb, 5);
413 if (s->time_offset_length[i] > 0) {
414 s->time_offset_value[i] = get_bits_long(gb, s->time_offset_length[i]);
415 }
416 }
417 }
418
419 64 s->present = 1;
420 64 return 0;
421 }
422
423 static int decode_film_grain_characteristics(HEVCSEIFilmGrainCharacteristics *h,
424 GetBitContext *gb)
425 {
426 h->present = !get_bits1(gb); // film_grain_characteristics_cancel_flag
427
428 if (h->present) {
429 memset(h, 0, sizeof(*h));
430 h->model_id = get_bits(gb, 2);
431 h->separate_colour_description_present_flag = get_bits1(gb);
432 if (h->separate_colour_description_present_flag) {
433 h->bit_depth_luma = get_bits(gb, 3) + 8;
434 h->bit_depth_chroma = get_bits(gb, 3) + 8;
435 h->full_range = get_bits1(gb);
436 h->color_primaries = get_bits(gb, 8);
437 h->transfer_characteristics = get_bits(gb, 8);
438 h->matrix_coeffs = get_bits(gb, 8);
439 }
440 h->blending_mode_id = get_bits(gb, 2);
441 h->log2_scale_factor = get_bits(gb, 4);
442 for (int c = 0; c < 3; c++)
443 h->comp_model_present_flag[c] = get_bits1(gb);
444 for (int c = 0; c < 3; c++) {
445 if (h->comp_model_present_flag[c]) {
446 h->num_intensity_intervals[c] = get_bits(gb, 8) + 1;
447 h->num_model_values[c] = get_bits(gb, 3) + 1;
448 if (h->num_model_values[c] > 6)
449 return AVERROR_INVALIDDATA;
450 for (int i = 0; i < h->num_intensity_intervals[c]; i++) {
451 h->intensity_interval_lower_bound[c][i] = get_bits(gb, 8);
452 h->intensity_interval_upper_bound[c][i] = get_bits(gb, 8);
453 for (int j = 0; j < h->num_model_values[c]; j++)
454 h->comp_model_value[c][i][j] = get_se_golomb_long(gb);
455 }
456 }
457 }
458 h->persistence_flag = get_bits1(gb);
459
460 h->present = 1;
461 }
462
463 return 0;
464 }
465
466 1489 static int decode_nal_sei_prefix(GetBitContext *gb, GetByteContext *gbyte,
467 void *logctx, HEVCSEI *s,
468 const HEVCParamSets *ps, int type)
469 {
470
8/13
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1204 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 30 times.
✓ Branch 7 taken 70 times.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 64 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 96 times.
1489 switch (type) {
471 case 256: // Mismatched value from HM 8.1
472 return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
473 case SEI_TYPE_FRAME_PACKING_ARRANGEMENT:
474 return decode_nal_sei_frame_packing_arrangement(&s->frame_packing, gb);
475 case SEI_TYPE_DISPLAY_ORIENTATION:
476 return decode_nal_sei_display_orientation(&s->display_orientation, gb);
477 1204 case SEI_TYPE_PIC_TIMING:
478 1204 return decode_nal_sei_pic_timing(s, gb, ps, logctx);
479 6 case SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
480 6 return decode_nal_sei_mastering_display_info(&s->mastering_display, gbyte);
481 6 case SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
482 6 return decode_nal_sei_content_light_info(&s->content_light, gbyte);
483 30 case SEI_TYPE_ACTIVE_PARAMETER_SETS:
484 30 return decode_nal_sei_active_parameter_sets(s, gb, logctx);
485 70 case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
486 70 return decode_nal_sei_user_data_registered_itu_t_t35(s, gbyte, logctx);
487 13 case SEI_TYPE_USER_DATA_UNREGISTERED:
488 13 return decode_nal_sei_user_data_unregistered(&s->unregistered, gbyte);
489 case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
490 return decode_nal_sei_alternative_transfer(&s->alternative_transfer, gbyte);
491 64 case SEI_TYPE_TIME_CODE:
492 64 return decode_nal_sei_timecode(&s->timecode, gb);
493 case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
494 return decode_film_grain_characteristics(&s->film_grain_characteristics, gb);
495 96 default:
496 96 av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
497 96 return 0;
498 }
499 }
500
501 6852 static int decode_nal_sei_suffix(GetBitContext *gb, GetByteContext *gbyte,
502 void *logctx, HEVCSEI *s, int type)
503 {
504
1/2
✓ Branch 0 taken 6852 times.
✗ Branch 1 not taken.
6852 switch (type) {
505 6852 case SEI_TYPE_DECODED_PICTURE_HASH:
506 6852 return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
507 default:
508 av_log(logctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
509 return 0;
510 }
511 }
512
513 8341 static int decode_nal_sei_message(GetByteContext *gb, void *logctx, HEVCSEI *s,
514 const HEVCParamSets *ps, int nal_unit_type)
515 {
516 GetByteContext message_gbyte;
517 GetBitContext message_gb;
518 8341 int payload_type = 0;
519 8341 int payload_size = 0;
520 8341 int byte = 0xFF;
521 av_unused int ret;
522 8341 av_log(logctx, AV_LOG_DEBUG, "Decoding SEI\n");
523
524
2/2
✓ Branch 0 taken 8341 times.
✓ Branch 1 taken 8341 times.
16682 while (byte == 0xFF) {
525
2/4
✓ Branch 1 taken 8341 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 8341 times.
8341 if (bytestream2_get_bytes_left(gb) < 2 || payload_type > INT_MAX - 255)
526 return AVERROR_INVALIDDATA;
527 8341 byte = bytestream2_get_byteu(gb);
528 8341 payload_type += byte;
529 }
530 8341 byte = 0xFF;
531
2/2
✓ Branch 0 taken 8408 times.
✓ Branch 1 taken 8341 times.
16749 while (byte == 0xFF) {
532
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8408 times.
8408 if (bytestream2_get_bytes_left(gb) < 1 + payload_size)
533 return AVERROR_INVALIDDATA;
534 8408 byte = bytestream2_get_byteu(gb);
535 8408 payload_size += byte;
536 }
537
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8341 times.
8341 if (bytestream2_get_bytes_left(gb) < payload_size)
538 return AVERROR_INVALIDDATA;
539 8341 bytestream2_init(&message_gbyte, gb->buffer, payload_size);
540 8341 ret = init_get_bits8(&message_gb, gb->buffer, payload_size);
541 av_assert1(ret >= 0);
542 8341 bytestream2_skipu(gb, payload_size);
543
2/2
✓ Branch 0 taken 1489 times.
✓ Branch 1 taken 6852 times.
8341 if (nal_unit_type == HEVC_NAL_SEI_PREFIX) {
544 1489 return decode_nal_sei_prefix(&message_gb, &message_gbyte,
545 logctx, s, ps, payload_type);
546 } else { /* nal_unit_type == NAL_SEI_SUFFIX */
547 6852 return decode_nal_sei_suffix(&message_gb, &message_gbyte,
548 logctx, s, payload_type);
549 }
550 }
551
552 8332 int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s,
553 const HEVCParamSets *ps, enum HEVCNALUnitType type)
554 {
555 GetByteContext gbyte;
556 int ret;
557
558 av_assert1((get_bits_count(gb) % 8) == 0);
559 8332 bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8,
560 8332 get_bits_left(gb) / 8);
561
562 do {
563 8341 ret = decode_nal_sei_message(&gbyte, logctx, s, ps, type);
564
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 8297 times.
8341 if (ret < 0)
565 44 return ret;
566
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 8288 times.
8297 } while (bytestream2_get_bytes_left(&gbyte) > 0);
567 8288 return 1;
568 }
569
570 12466 void ff_hevc_reset_sei(HEVCSEI *s)
571 {
572 12466 av_buffer_unref(&s->a53_caption.buf_ref);
573
574
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12466 times.
12470 for (int i = 0; i < s->unregistered.nb_buf_ref; i++)
575 4 av_buffer_unref(&s->unregistered.buf_ref[i]);
576 12466 s->unregistered.nb_buf_ref = 0;
577 12466 av_freep(&s->unregistered.buf_ref);
578 12466 av_buffer_unref(&s->dynamic_hdr_plus.info);
579 12466 av_buffer_unref(&s->dynamic_hdr_vivid.info);
580 12466 }
581