FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_sei.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 255 487 52.4%
Functions: 16 20 80.0%
Branches: 114 275 41.5%

Line Branch Exec Source
1 /*
2 * Common H.264 and HEVC Supplementary Enhancement Information messages
3 *
4 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * Copyright (C) 2012 - 2013 Gildas Cocherel
7 * Copyright (C) 2013 Vittorio Giovara
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/ambient_viewing_environment.h"
29 #include "libavutil/display.h"
30 #include "libavutil/hdr_dynamic_metadata.h"
31 #include "libavutil/film_grain_params.h"
32 #include "libavutil/mastering_display_metadata.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/stereo3d.h"
35
36 #include "atsc_a53.h"
37 #include "avcodec.h"
38 #include "decode.h"
39 #include "dynamic_hdr_vivid.h"
40 #include "get_bits.h"
41 #include "golomb.h"
42 #include "h2645_sei.h"
43 #include "itut35.h"
44
45 #define IS_H264(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_H264 : CONFIG_H264_SEI)
46 #define IS_HEVC(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_HEVC : CONFIG_HEVC_SEI)
47
48 #if CONFIG_HEVC_SEI
49 3 static int decode_registered_user_data_dynamic_hdr_plus(HEVCSEIDynamicHDRPlus *s,
50 GetByteContext *gb)
51 {
52 size_t meta_size;
53 int err;
54 3 AVDynamicHDRPlus *metadata = av_dynamic_hdr_plus_alloc(&meta_size);
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!metadata)
56 return AVERROR(ENOMEM);
57
58 3 err = av_dynamic_hdr_plus_from_t35(metadata, gb->buffer,
59 3 bytestream2_get_bytes_left(gb));
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0) {
61 av_free(metadata);
62 return err;
63 }
64
65 3 av_buffer_unref(&s->info);
66 3 s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->info) {
68 av_free(metadata);
69 return AVERROR(ENOMEM);
70 }
71
72 3 return 0;
73 }
74
75 3 static int decode_registered_user_data_dynamic_hdr_vivid(HEVCSEIDynamicHDRVivid *s,
76 GetByteContext *gb)
77 {
78 size_t meta_size;
79 int err;
80 3 AVDynamicHDRVivid *metadata = av_dynamic_hdr_vivid_alloc(&meta_size);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!metadata)
82 return AVERROR(ENOMEM);
83
84 3 err = ff_parse_itu_t_t35_to_dynamic_hdr_vivid(metadata,
85 gb->buffer, bytestream2_get_bytes_left(gb));
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0) {
87 av_free(metadata);
88 return err;
89 }
90
91 3 av_buffer_unref(&s->info);
92 3 s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->info) {
94 av_free(metadata);
95 return AVERROR(ENOMEM);
96 }
97
98 3 return 0;
99 }
100 #endif
101
102 1136 static int decode_registered_user_data_afd(H2645SEIAFD *h, GetByteContext *gb)
103 {
104 int flag;
105
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1136 times.
1136 if (bytestream2_get_bytes_left(gb) <= 0)
107 return AVERROR_INVALIDDATA;
108
109 1136 flag = !!(bytestream2_get_byteu(gb) & 0x40); // active_format_flag
110
111
1/2
✓ Branch 0 taken 1136 times.
✗ Branch 1 not taken.
1136 if (flag) {
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1136 times.
1136 if (bytestream2_get_bytes_left(gb) <= 0)
113 return AVERROR_INVALIDDATA;
114 1136 h->active_format_description = bytestream2_get_byteu(gb) & 0xF;
115 1136 h->present = 1;
116 }
117
118 1136 return 0;
119 }
120
121 6 static int decode_registered_user_data_closed_caption(H2645SEIA53Caption *h,
122 GetByteContext *gb)
123 {
124 6 return ff_parse_a53_cc(&h->buf_ref, gb->buffer,
125 bytestream2_get_bytes_left(gb));
126 }
127
128 1148 static int decode_registered_user_data(H2645SEI *h, GetByteContext *gb,
129 enum AVCodecID codec_id, void *logctx)
130 {
131 int country_code, provider_code;
132
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1148 times.
1148 if (bytestream2_get_bytes_left(gb) < 3)
134 return AVERROR_INVALIDDATA;
135
136 1148 country_code = bytestream2_get_byteu(gb); // itu_t_t35_country_code
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1148 times.
1148 if (country_code == 0xFF) {
138 if (bytestream2_get_bytes_left(gb) < 3)
139 return AVERROR_INVALIDDATA;
140
141 bytestream2_skipu(gb, 1); // itu_t_t35_country_code_extension_byte
142 }
143
144
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1145 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
1148 if (country_code != ITU_T_T35_COUNTRY_CODE_US &&
145 country_code != ITU_T_T35_COUNTRY_CODE_CN) {
146 av_log(logctx, AV_LOG_VERBOSE,
147 "Unsupported User Data Registered ITU-T T35 SEI message (country_code = %d)\n",
148 country_code);
149 return 0;
150 }
151
152 /* itu_t_t35_payload_byte follows */
153 1148 provider_code = bytestream2_get_be16u(gb);
154
155
3/5
✓ Branch 0 taken 1142 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1148 switch (provider_code) {
156 1142 case ITU_T_T35_PROVIDER_CODE_ATSC: {
157 uint32_t user_identifier;
158
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1142 times.
1142 if (bytestream2_get_bytes_left(gb) < 4)
160 return AVERROR_INVALIDDATA;
161
162 1142 user_identifier = bytestream2_get_be32u(gb);
163 switch (user_identifier) {
164 1136 case MKBETAG('D', 'T', 'G', '1'): // afd_data
165 1136 return decode_registered_user_data_afd(&h->afd, gb);
166 6 case MKBETAG('G', 'A', '9', '4'): // closed captions
167 6 return decode_registered_user_data_closed_caption(&h->a53_caption, gb);
168 default:
169 av_log(logctx, AV_LOG_VERBOSE,
170 "Unsupported User Data Registered ITU-T T35 SEI message (atsc user_identifier = 0x%04x)\n",
171 user_identifier);
172 break;
173 }
174 break;
175 }
176 #if CONFIG_HEVC_SEI
177 3 case ITU_T_T35_PROVIDER_CODE_CUVA: {
178 3 const uint16_t cuva_provider_oriented_code = 0x0005;
179 uint16_t provider_oriented_code;
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!IS_HEVC(codec_id))
182 goto unsupported_provider_code;
183
184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(gb) < 2)
185 return AVERROR_INVALIDDATA;
186
187 3 provider_oriented_code = bytestream2_get_be16u(gb);
188
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (provider_oriented_code == cuva_provider_oriented_code) {
189 3 return decode_registered_user_data_dynamic_hdr_vivid(&h->dynamic_hdr_vivid, gb);
190 }
191 break;
192 }
193 3 case ITU_T_T35_PROVIDER_CODE_SMTPE: {
194 // A/341 Amendment - 2094-40
195 3 const uint16_t smpte2094_40_provider_oriented_code = 0x0001;
196 3 const uint8_t smpte2094_40_application_identifier = 0x04;
197 uint16_t provider_oriented_code;
198 uint8_t application_identifier;
199
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!IS_HEVC(codec_id))
201 goto unsupported_provider_code;
202
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(gb) < 3)
204 return AVERROR_INVALIDDATA;
205
206 3 provider_oriented_code = bytestream2_get_be16u(gb);
207 3 application_identifier = bytestream2_get_byteu(gb);
208
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 &&
209 application_identifier == smpte2094_40_application_identifier) {
210 3 return decode_registered_user_data_dynamic_hdr_plus(&h->dynamic_hdr_plus, gb);
211 }
212 break;
213 }
214 case 0x5890: { // aom_provider_code
215 const uint16_t aom_grain_provider_oriented_code = 0x0001;
216 uint16_t provider_oriented_code;
217
218 if (!IS_HEVC(codec_id))
219 goto unsupported_provider_code;
220
221 if (bytestream2_get_bytes_left(gb) < 2)
222 return AVERROR_INVALIDDATA;
223
224 provider_oriented_code = bytestream2_get_byteu(gb);
225 if (provider_oriented_code == aom_grain_provider_oriented_code) {
226 return ff_aom_parse_film_grain_sets(&h->aom_film_grain,
227 gb->buffer,
228 bytestream2_get_bytes_left(gb));
229 }
230 break;
231 }
232 unsupported_provider_code:
233 #endif
234 default:
235 av_log(logctx, AV_LOG_VERBOSE,
236 "Unsupported User Data Registered ITU-T T35 SEI message (provider_code = %d)\n",
237 provider_code);
238 break;
239 }
240
241 return 0;
242 }
243
244 590 static int decode_unregistered_user_data(H2645SEIUnregistered *h,
245 GetByteContext *gb,
246 enum AVCodecID codec_id)
247 {
248 uint8_t *user_data;
249 590 int size = bytestream2_get_bytes_left(gb);
250 AVBufferRef *buf_ref, **tmp;
251
252
2/4
✓ Branch 0 taken 590 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 590 times.
590 if (size < 16 || size >= INT_MAX - 1)
253 return AVERROR_INVALIDDATA;
254
255 590 tmp = av_realloc_array(h->buf_ref, h->nb_buf_ref + 1, sizeof(*h->buf_ref));
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 590 times.
590 if (!tmp)
257 return AVERROR(ENOMEM);
258 590 h->buf_ref = tmp;
259
260 590 buf_ref = av_buffer_alloc(size + 1);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 590 times.
590 if (!buf_ref)
262 return AVERROR(ENOMEM);
263 590 user_data = buf_ref->data;
264
265 590 bytestream2_get_bufferu(gb, user_data, size);
266 590 user_data[size] = 0;
267 590 buf_ref->size = size;
268 590 h->buf_ref[h->nb_buf_ref++] = buf_ref;
269
270
2/2
✓ Branch 0 taken 570 times.
✓ Branch 1 taken 20 times.
590 if (IS_H264(codec_id)) {
271 int e, build;
272 570 e = sscanf(user_data + 16, "x264 - core %d", &build);
273
4/4
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 427 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 3 times.
570 if (e == 1 && build > 0)
274 140 h->x264_build = build;
275
3/6
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 427 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 143 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
570 if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
276 h->x264_build = 67;
277 }
278
279 590 return 0;
280 }
281
282 2 static int decode_display_orientation(H2645SEIDisplayOrientation *h,
283 GetBitContext *gb)
284 {
285 2 h->present = !get_bits1(gb); // display_orientation_cancel_flag
286
287
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (h->present) {
288 2 h->hflip = get_bits1(gb); // hor_flip
289 2 h->vflip = get_bits1(gb); // ver_flip
290
291 2 h->anticlockwise_rotation = get_bits(gb, 16);
292 // This is followed by display_orientation_repetition_period
293 // and display_orientation_extension_flag for H.264
294 // and by display_orientation_persistence_flag for HEVC.
295 }
296
297 2 return 0;
298 }
299
300 static int decode_frame_packing_arrangement(H2645SEIFramePacking *h,
301 GetBitContext *gb,
302 enum AVCodecID codec_id)
303 {
304 h->arrangement_id = get_ue_golomb_long(gb);
305 h->arrangement_cancel_flag = get_bits1(gb);
306 h->present = !h->arrangement_cancel_flag;
307
308 if (h->present) {
309 h->arrangement_type = get_bits(gb, 7);
310 h->quincunx_sampling_flag = get_bits1(gb);
311 h->content_interpretation_type = get_bits(gb, 6);
312
313 // spatial_flipping_flag, frame0_flipped_flag, field_views_flag
314 skip_bits(gb, 3);
315 h->current_frame_is_frame0_flag = get_bits1(gb);
316 // frame0_self_contained_flag, frame1_self_contained_flag
317 skip_bits(gb, 2);
318
319 if (!h->quincunx_sampling_flag && h->arrangement_type != 5)
320 skip_bits(gb, 16); // frame[01]_grid_position_[xy]
321 skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
322 if (IS_H264(codec_id))
323 h->arrangement_repetition_period = get_ue_golomb_long(gb);
324 else
325 skip_bits1(gb); // frame_packing_arrangement_persistence_flag
326 }
327 // H.264: frame_packing_arrangement_extension_flag,
328 // HEVC: upsampled_aspect_ratio_flag
329 skip_bits1(gb);
330
331 return 0;
332 }
333
334 static int decode_alternative_transfer(H2645SEIAlternativeTransfer *s,
335 GetByteContext *gb)
336 {
337 if (bytestream2_get_bytes_left(gb) < 1)
338 return AVERROR_INVALIDDATA;
339
340 s->present = 1;
341 s->preferred_transfer_characteristics = bytestream2_get_byteu(gb);
342
343 return 0;
344 }
345
346 9 static int decode_ambient_viewing_environment(H2645SEIAmbientViewingEnvironment *s,
347 GetByteContext *gb)
348 {
349 static const uint16_t max_ambient_light_value = 50000;
350
351
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (bytestream2_get_bytes_left(gb) < 8)
352 return AVERROR_INVALIDDATA;
353
354 9 s->ambient_illuminance = bytestream2_get_be32u(gb);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!s->ambient_illuminance)
356 return AVERROR_INVALIDDATA;
357
358 9 s->ambient_light_x = bytestream2_get_be16u(gb);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->ambient_light_x > max_ambient_light_value)
360 return AVERROR_INVALIDDATA;
361
362 9 s->ambient_light_y = bytestream2_get_be16u(gb);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->ambient_light_y > max_ambient_light_value)
364 return AVERROR_INVALIDDATA;
365
366 9 s->present = 1;
367
368 9 return 0;
369 }
370
371 static int decode_film_grain_characteristics(H2645SEIFilmGrainCharacteristics *h,
372 enum AVCodecID codec_id, GetBitContext *gb)
373 {
374 h->present = !get_bits1(gb); // film_grain_characteristics_cancel_flag
375
376 if (h->present) {
377 memset(h, 0, sizeof(*h));
378 h->model_id = get_bits(gb, 2);
379 h->separate_colour_description_present_flag = get_bits1(gb);
380 if (h->separate_colour_description_present_flag) {
381 h->bit_depth_luma = get_bits(gb, 3) + 8;
382 h->bit_depth_chroma = get_bits(gb, 3) + 8;
383 h->full_range = get_bits1(gb);
384 h->color_primaries = get_bits(gb, 8);
385 h->transfer_characteristics = get_bits(gb, 8);
386 h->matrix_coeffs = get_bits(gb, 8);
387 }
388 h->blending_mode_id = get_bits(gb, 2);
389 h->log2_scale_factor = get_bits(gb, 4);
390 for (int c = 0; c < 3; c++)
391 h->comp_model_present_flag[c] = get_bits1(gb);
392 for (int c = 0; c < 3; c++) {
393 if (h->comp_model_present_flag[c]) {
394 h->num_intensity_intervals[c] = get_bits(gb, 8) + 1;
395 h->num_model_values[c] = get_bits(gb, 3) + 1;
396 if (h->num_model_values[c] > 6)
397 return AVERROR_INVALIDDATA;
398 for (int i = 0; i < h->num_intensity_intervals[c]; i++) {
399 h->intensity_interval_lower_bound[c][i] = get_bits(gb, 8);
400 h->intensity_interval_upper_bound[c][i] = get_bits(gb, 8);
401 for (int j = 0; j < h->num_model_values[c]; j++)
402 h->comp_model_value[c][i][j] = get_se_golomb_long(gb);
403 }
404 }
405 }
406 if (IS_HEVC(codec_id))
407 h->persistence_flag = get_bits1(gb);
408 else
409 h->repetition_period = get_ue_golomb_long(gb);
410
411 h->present = 1;
412 }
413
414 return 0;
415 }
416
417 15 static int decode_nal_sei_mastering_display_info(H2645SEIMasteringDisplay *s,
418 GetByteContext *gb)
419 {
420 int i;
421
422
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (bytestream2_get_bytes_left(gb) < 24)
423 return AVERROR_INVALIDDATA;
424
425 // Mastering primaries
426
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 15 times.
60 for (i = 0; i < 3; i++) {
427 45 s->display_primaries[i][0] = bytestream2_get_be16u(gb);
428 45 s->display_primaries[i][1] = bytestream2_get_be16u(gb);
429 }
430 // White point (x, y)
431 15 s->white_point[0] = bytestream2_get_be16u(gb);
432 15 s->white_point[1] = bytestream2_get_be16u(gb);
433
434 // Max and min luminance of mastering display
435 15 s->max_luminance = bytestream2_get_be32u(gb);
436 15 s->min_luminance = bytestream2_get_be32u(gb);
437
438 // As this SEI message comes before the first frame that references it,
439 // initialize the flag to 2 and decrement on IRAP access unit so it
440 // persists for the coded video sequence (e.g., between two IRAPs)
441 15 s->present = 2;
442
443 15 return 0;
444 }
445
446 6 static int decode_nal_sei_content_light_info(H2645SEIContentLight *s,
447 GetByteContext *gb)
448 {
449
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_bytes_left(gb) < 4)
450 return AVERROR_INVALIDDATA;
451
452 // Max and average light levels
453 6 s->max_content_light_level = bytestream2_get_be16u(gb);
454 6 s->max_pic_average_light_level = bytestream2_get_be16u(gb);
455 // As this SEI message comes before the first frame that references it,
456 // initialize the flag to 2 and decrement on IRAP access unit so it
457 // persists for the coded video sequence (e.g., between two IRAPs)
458 6 s->present = 2;
459
460 6 return 0;
461 }
462
463 2200 int ff_h2645_sei_message_decode(H2645SEI *h, enum SEIType type,
464 enum AVCodecID codec_id, GetBitContext *gb,
465 GetByteContext *gbyte, void *logctx)
466 {
467
7/10
✓ Branch 0 taken 1148 times.
✓ Branch 1 taken 590 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 15 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 430 times.
2200 switch (type) {
468 1148 case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
469 1148 return decode_registered_user_data(h, gbyte, codec_id, logctx);
470 590 case SEI_TYPE_USER_DATA_UNREGISTERED:
471 590 return decode_unregistered_user_data(&h->unregistered, gbyte, codec_id);
472 2 case SEI_TYPE_DISPLAY_ORIENTATION:
473 2 return decode_display_orientation(&h->display_orientation, gb);
474 case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
475 return decode_film_grain_characteristics(&h->film_grain_characteristics, codec_id, gb);
476 case SEI_TYPE_FRAME_PACKING_ARRANGEMENT:
477 return decode_frame_packing_arrangement(&h->frame_packing, gb, codec_id);
478 case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
479 return decode_alternative_transfer(&h->alternative_transfer, gbyte);
480 9 case SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT:
481 9 return decode_ambient_viewing_environment(&h->ambient_viewing_environment,
482 gbyte);
483 15 case SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
484 15 return decode_nal_sei_mastering_display_info(&h->mastering_display,
485 gbyte);
486 6 case SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
487 6 return decode_nal_sei_content_light_info(&h->content_light, gbyte);
488 430 default:
489 430 return FF_H2645_SEI_MESSAGE_UNHANDLED;
490 }
491 }
492
493 94 int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src)
494 {
495 94 int ret = av_buffer_replace(&dst->a53_caption.buf_ref,
496 94 src->a53_caption.buf_ref);
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (ret < 0)
498 return ret;
499
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 for (unsigned i = 0; i < dst->unregistered.nb_buf_ref; i++)
501 av_buffer_unref(&dst->unregistered.buf_ref[i]);
502 94 dst->unregistered.nb_buf_ref = 0;
503
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (src->unregistered.nb_buf_ref) {
505 ret = av_reallocp_array(&dst->unregistered.buf_ref,
506 src->unregistered.nb_buf_ref,
507 sizeof(*dst->unregistered.buf_ref));
508 if (ret < 0)
509 return ret;
510
511 for (unsigned i = 0; i < src->unregistered.nb_buf_ref; i++) {
512 dst->unregistered.buf_ref[i] = av_buffer_ref(src->unregistered.buf_ref[i]);
513 if (!dst->unregistered.buf_ref[i])
514 return AVERROR(ENOMEM);
515 dst->unregistered.nb_buf_ref++;
516 }
517 }
518
519 94 return 0;
520 }
521
522 static int is_frame_packing_type_valid(SEIFpaType type, enum AVCodecID codec_id)
523 {
524 if (IS_H264(codec_id))
525 return type <= SEI_FPA_H264_TYPE_2D &&
526 type >= SEI_FPA_H264_TYPE_CHECKERBOARD;
527 else
528 return type <= SEI_FPA_TYPE_INTERLEAVE_TEMPORAL &&
529 type >= SEI_FPA_TYPE_SIDE_BY_SIDE;
530 }
531
532 34692 static int h2645_sei_to_side_data(AVCodecContext *avctx, H2645SEI *sei,
533 AVFrameSideData ***sd, int *nb_sd)
534 {
535 int ret;
536
537
2/2
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 34692 times.
34986 for (unsigned i = 0; i < sei->unregistered.nb_buf_ref; i++) {
538 294 H2645SEIUnregistered *unreg = &sei->unregistered;
539
540
1/2
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
294 if (unreg->buf_ref[i]) {
541 AVFrameSideData *entry =
542 294 av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_SEI_UNREGISTERED,
543 294 &unreg->buf_ref[i], 0);
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294 times.
294 if (!entry)
545 av_buffer_unref(&unreg->buf_ref[i]);
546 }
547 }
548 34692 sei->unregistered.nb_buf_ref = 0;
549
550
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 34682 times.
34692 if (sei->ambient_viewing_environment.present) {
551 10 H2645SEIAmbientViewingEnvironment *env = &sei->ambient_viewing_environment;
552 AVBufferRef *buf;
553 size_t size;
554
555 AVAmbientViewingEnvironment *dst_env =
556 10 av_ambient_viewing_environment_alloc(&size);
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!dst_env)
558 return AVERROR(ENOMEM);
559
560 10 buf = av_buffer_create((uint8_t *)dst_env, size, NULL, NULL, 0);
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!buf) {
562 av_free(dst_env);
563 return AVERROR(ENOMEM);
564 }
565
566 10 ret = ff_frame_new_side_data_from_buf_ext(avctx, sd, nb_sd,
567 AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT, &buf);
568
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
570 return ret;
571
572 10 dst_env->ambient_illuminance = av_make_q(env->ambient_illuminance, 10000);
573 10 dst_env->ambient_light_x = av_make_q(env->ambient_light_x, 50000);
574 10 dst_env->ambient_light_y = av_make_q(env->ambient_light_y, 50000);
575 }
576
577
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 34661 times.
34692 if (sei->mastering_display.present) {
578 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
579 31 const int mapping[3] = {2, 0, 1};
580 31 const int chroma_den = 50000;
581 31 const int luma_den = 10000;
582 int i;
583 AVMasteringDisplayMetadata *metadata;
584
585 31 ret = ff_decode_mastering_display_new_ext(avctx, sd, nb_sd, &metadata);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
587 return ret;
588
589
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 10 times.
31 if (metadata) {
590 21 metadata->has_luminance = 1;
591 21 metadata->has_primaries = 1;
592
593
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
84 for (i = 0; i < 3; i++) {
594 63 const int j = mapping[i];
595 63 metadata->display_primaries[i][0].num = sei->mastering_display.display_primaries[j][0];
596 63 metadata->display_primaries[i][0].den = chroma_den;
597
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
126 metadata->has_primaries &= sei->mastering_display.display_primaries[j][0] >= 5 &&
598
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 sei->mastering_display.display_primaries[j][0] <= 37000;
599
600 63 metadata->display_primaries[i][1].num = sei->mastering_display.display_primaries[j][1];
601 63 metadata->display_primaries[i][1].den = chroma_den;
602
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
126 metadata->has_primaries &= sei->mastering_display.display_primaries[j][1] >= 5 &&
603
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 sei->mastering_display.display_primaries[j][1] <= 42000;
604 }
605 21 metadata->white_point[0].num = sei->mastering_display.white_point[0];
606 21 metadata->white_point[0].den = chroma_den;
607
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
42 metadata->has_primaries &= sei->mastering_display.white_point[0] >= 5 &&
608
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 sei->mastering_display.white_point[0] <= 37000;
609
610 21 metadata->white_point[1].num = sei->mastering_display.white_point[1];
611 21 metadata->white_point[1].den = chroma_den;
612
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
42 metadata->has_primaries &= sei->mastering_display.white_point[1] >= 5 &&
613
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 sei->mastering_display.white_point[1] <= 42000;
614
615 21 metadata->max_luminance.num = sei->mastering_display.max_luminance;
616 21 metadata->max_luminance.den = luma_den;
617
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
42 metadata->has_luminance &= sei->mastering_display.max_luminance >= 50000 &&
618
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 sei->mastering_display.max_luminance <= 100000000;
619
620 21 metadata->min_luminance.num = sei->mastering_display.min_luminance;
621 21 metadata->min_luminance.den = luma_den;
622
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
42 metadata->has_luminance &= sei->mastering_display.min_luminance <= 50000 &&
623 21 sei->mastering_display.min_luminance <
624
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 sei->mastering_display.max_luminance;
625
626 /* Real (blu-ray) releases in the wild come with minimum luminance
627 * values of 0.000 cd/m2, so permit this edge case */
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT)
629 metadata->has_luminance &= sei->mastering_display.min_luminance >= 1;
630
631
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
21 if (metadata->has_luminance || metadata->has_primaries)
632 21 av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
633
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (metadata->has_primaries) {
634 21 av_log(avctx, AV_LOG_DEBUG,
635 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
636 21 av_q2d(metadata->display_primaries[0][0]),
637 21 av_q2d(metadata->display_primaries[0][1]),
638 21 av_q2d(metadata->display_primaries[1][0]),
639 21 av_q2d(metadata->display_primaries[1][1]),
640 21 av_q2d(metadata->display_primaries[2][0]),
641 21 av_q2d(metadata->display_primaries[2][1]),
642 21 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
643 }
644
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (metadata->has_luminance) {
645 20 av_log(avctx, AV_LOG_DEBUG,
646 "min_luminance=%f, max_luminance=%f\n",
647 20 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
648 }
649 }
650 }
651
652
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 34688 times.
34692 if (sei->content_light.present) {
653 AVContentLightMetadata *metadata;
654
655 4 ret = ff_decode_content_light_new_ext(avctx, sd, nb_sd, &metadata);
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
657 return ret;
658
659
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (metadata) {
660 4 metadata->MaxCLL = sei->content_light.max_content_light_level;
661 4 metadata->MaxFALL = sei->content_light.max_pic_average_light_level;
662
663 4 av_log(avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
664 4 av_log(avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
665 4 metadata->MaxCLL, metadata->MaxFALL);
666 }
667 }
668
669 34692 return 0;
670 }
671
672 34462 int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
673 enum AVCodecID codec_id,
674 AVCodecContext *avctx, const H2645VUI *vui,
675 unsigned bit_depth_luma, unsigned bit_depth_chroma,
676 int seed)
677 {
678 34462 H2645SEIFramePacking *fp = &sei->frame_packing;
679 int ret;
680
681
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34462 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34462 if (fp->present &&
682 is_frame_packing_type_valid(fp->arrangement_type, codec_id) &&
683 fp->content_interpretation_type > 0 &&
684 fp->content_interpretation_type < 3) {
685 AVStereo3D *stereo = av_stereo3d_create_side_data(frame);
686
687 if (!stereo)
688 return AVERROR(ENOMEM);
689
690 switch (fp->arrangement_type) {
691 #if CONFIG_H264_SEI
692 case SEI_FPA_H264_TYPE_CHECKERBOARD:
693 stereo->type = AV_STEREO3D_CHECKERBOARD;
694 break;
695 case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN:
696 stereo->type = AV_STEREO3D_COLUMNS;
697 break;
698 case SEI_FPA_H264_TYPE_INTERLEAVE_ROW:
699 stereo->type = AV_STEREO3D_LINES;
700 break;
701 #endif
702 case SEI_FPA_TYPE_SIDE_BY_SIDE:
703 if (fp->quincunx_sampling_flag)
704 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
705 else
706 stereo->type = AV_STEREO3D_SIDEBYSIDE;
707 break;
708 case SEI_FPA_TYPE_TOP_BOTTOM:
709 stereo->type = AV_STEREO3D_TOPBOTTOM;
710 break;
711 case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
712 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
713 break;
714 #if CONFIG_H264_SEI
715 case SEI_FPA_H264_TYPE_2D:
716 stereo->type = AV_STEREO3D_2D;
717 break;
718 #endif
719 }
720
721 if (fp->content_interpretation_type == 2)
722 stereo->flags = AV_STEREO3D_FLAG_INVERT;
723
724 if (fp->arrangement_type == SEI_FPA_TYPE_INTERLEAVE_TEMPORAL) {
725 if (fp->current_frame_is_frame0_flag)
726 stereo->view = AV_STEREO3D_VIEW_LEFT;
727 else
728 stereo->view = AV_STEREO3D_VIEW_RIGHT;
729 }
730 }
731
732
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34461 times.
34462 if (sei->display_orientation.present &&
733
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (sei->display_orientation.anticlockwise_rotation ||
734
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 sei->display_orientation.hflip ||
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 sei->display_orientation.vflip)) {
736 H2645SEIDisplayOrientation *o = &sei->display_orientation;
737 double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
738 AVFrameSideData *rotation = av_frame_new_side_data(frame,
739 AV_FRAME_DATA_DISPLAYMATRIX,
740 sizeof(int32_t) * 9);
741 if (!rotation)
742 return AVERROR(ENOMEM);
743
744 /* av_display_rotation_set() expects the angle in the clockwise
745 * direction, hence the first minus.
746 * The below code applies the flips after the rotation, yet
747 * the H.2645 specs require flipping to be applied first.
748 * Because of R O(phi) = O(-phi) R (where R is flipping around
749 * an arbitatry axis and O(phi) is the proper rotation by phi)
750 * we can create display matrices as desired by negating
751 * the degree once for every flip applied. */
752 angle = -angle * (1 - 2 * !!o->hflip) * (1 - 2 * !!o->vflip);
753 av_display_rotation_set((int32_t *)rotation->data, angle);
754 av_display_matrix_flip((int32_t *)rotation->data,
755 o->hflip, o->vflip);
756 }
757
758
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34461 times.
34462 if (sei->a53_caption.buf_ref) {
759 1 H2645SEIA53Caption *a53 = &sei->a53_caption;
760 1 AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, a53->buf_ref);
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd)
762 av_buffer_unref(&a53->buf_ref);
763 1 a53->buf_ref = NULL;
764 1 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
765 }
766
767 34462 ret = h2645_sei_to_side_data(avctx, sei, &frame->side_data, &frame->nb_side_data);
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34462 times.
34462 if (ret < 0)
769 return ret;
770
771
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 34138 times.
34462 if (sei->afd.present) {
772 324 AVFrameSideData *sd = av_frame_new_side_data(frame, AV_FRAME_DATA_AFD,
773 sizeof(uint8_t));
774
775
1/2
✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
324 if (sd) {
776 324 *sd->data = sei->afd.active_format_description;
777 324 sei->afd.present = 0;
778 }
779 }
780
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34462 times.
34462 if (sei->film_grain_characteristics.present) {
782 H2645SEIFilmGrainCharacteristics *fgc = &sei->film_grain_characteristics;
783 AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
784 AVFilmGrainH274Params *h274;
785
786 if (!fgp)
787 return AVERROR(ENOMEM);
788
789 fgp->type = AV_FILM_GRAIN_PARAMS_H274;
790 h274 = &fgp->codec.h274;
791
792 fgp->seed = seed;
793 fgp->width = frame->width;
794 fgp->height = frame->height;
795
796 /* H.274 mandates film grain be applied to 4:4:4 frames */
797 fgp->subsampling_x = fgp->subsampling_y = 0;
798
799 h274->model_id = fgc->model_id;
800 if (fgc->separate_colour_description_present_flag) {
801 fgp->bit_depth_luma = fgc->bit_depth_luma;
802 fgp->bit_depth_chroma = fgc->bit_depth_chroma;
803 fgp->color_range = fgc->full_range + 1;
804 fgp->color_primaries = fgc->color_primaries;
805 fgp->color_trc = fgc->transfer_characteristics;
806 fgp->color_space = fgc->matrix_coeffs;
807 } else {
808 fgp->bit_depth_luma = bit_depth_luma;
809 fgp->bit_depth_chroma = bit_depth_chroma;
810 if (vui->video_signal_type_present_flag)
811 fgp->color_range = vui->video_full_range_flag + 1;
812 if (vui->colour_description_present_flag) {
813 fgp->color_primaries = vui->colour_primaries;
814 fgp->color_trc = vui->transfer_characteristics;
815 fgp->color_space = vui->matrix_coeffs;
816 }
817 }
818 h274->blending_mode_id = fgc->blending_mode_id;
819 h274->log2_scale_factor = fgc->log2_scale_factor;
820
821 #if FF_API_H274_FILM_GRAIN_VCS
822 FF_DISABLE_DEPRECATION_WARNINGS
823 h274->bit_depth_luma = fgp->bit_depth_luma;
824 h274->bit_depth_chroma = fgp->bit_depth_chroma;
825 h274->color_range = fgp->color_range;
826 h274->color_primaries = fgp->color_primaries;
827 h274->color_trc = fgp->color_trc;
828 h274->color_space = fgp->color_space;
829 FF_ENABLE_DEPRECATION_WARNINGS
830 #endif
831
832 memcpy(&h274->component_model_present, &fgc->comp_model_present_flag,
833 sizeof(h274->component_model_present));
834 memcpy(&h274->num_intensity_intervals, &fgc->num_intensity_intervals,
835 sizeof(h274->num_intensity_intervals));
836 memcpy(&h274->num_model_values, &fgc->num_model_values,
837 sizeof(h274->num_model_values));
838 memcpy(&h274->intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
839 sizeof(h274->intensity_interval_lower_bound));
840 memcpy(&h274->intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
841 sizeof(h274->intensity_interval_upper_bound));
842 memcpy(&h274->comp_model_value, &fgc->comp_model_value,
843 sizeof(h274->comp_model_value));
844
845 if (IS_H264(codec_id))
846 fgc->present = !!fgc->repetition_period;
847 else
848 fgc->present = fgc->persistence_flag;
849
850 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
851 }
852
853 #if CONFIG_HEVC_SEI
854 34462 ret = ff_aom_attach_film_grain_sets(&sei->aom_film_grain, frame);
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34462 times.
34462 if (ret < 0)
856 return ret;
857 #endif
858
859 34462 return 0;
860 }
861
862 230 int ff_h2645_sei_to_context(AVCodecContext *avctx, H2645SEI *sei)
863 {
864 230 return h2645_sei_to_side_data(avctx, sei, &avctx->decoded_side_data,
865 &avctx->nb_decoded_side_data);
866 }
867
868 73366 void ff_h2645_sei_reset(H2645SEI *s)
869 {
870 73366 av_buffer_unref(&s->a53_caption.buf_ref);
871
872
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 73366 times.
73662 for (unsigned i = 0; i < s->unregistered.nb_buf_ref; i++)
873 296 av_buffer_unref(&s->unregistered.buf_ref[i]);
874 73366 s->unregistered.nb_buf_ref = 0;
875 73366 av_freep(&s->unregistered.buf_ref);
876 73366 av_buffer_unref(&s->dynamic_hdr_plus.info);
877 73366 av_buffer_unref(&s->dynamic_hdr_vivid.info);
878
879 73366 s->ambient_viewing_environment.present = 0;
880 73366 s->mastering_display.present = 0;
881 73366 s->content_light.present = 0;
882 73366 s->aom_film_grain.enable = 0;
883 73366 }
884