FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_sei.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 256 488 52.5%
Functions: 16 20 80.0%
Branches: 116 279 41.6%

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 1169 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 1169 times.
1169 if (bytestream2_get_bytes_left(gb) <= 0)
107 return AVERROR_INVALIDDATA;
108
109 1169 flag = !!(bytestream2_get_byteu(gb) & 0x40); // active_format_flag
110
111
1/2
✓ Branch 0 taken 1169 times.
✗ Branch 1 not taken.
1169 if (flag) {
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1169 times.
1169 if (bytestream2_get_bytes_left(gb) <= 0)
113 return AVERROR_INVALIDDATA;
114 1169 h->active_format_description = bytestream2_get_byteu(gb) & 0xF;
115 1169 h->present = 1;
116 }
117
118 1169 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 1181 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 1181 times.
1181 if (bytestream2_get_bytes_left(gb) < 3)
134 return AVERROR_INVALIDDATA;
135
136 1181 country_code = bytestream2_get_byteu(gb); // itu_t_t35_country_code
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1181 times.
1181 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 1178 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
1181 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 1181 provider_code = bytestream2_get_be16u(gb);
154
155
3/5
✓ Branch 0 taken 1175 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1181 switch (provider_code) {
156 1175 case ITU_T_T35_PROVIDER_CODE_ATSC: {
157 uint32_t user_identifier;
158
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1175 times.
1175 if (bytestream2_get_bytes_left(gb) < 4)
160 return AVERROR_INVALIDDATA;
161
162 1175 user_identifier = bytestream2_get_be32u(gb);
163 switch (user_identifier) {
164 1169 case MKBETAG('D', 'T', 'G', '1'): // afd_data
165 1169 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 588 static int decode_unregistered_user_data(H2645SEIUnregistered *h,
245 GetByteContext *gb,
246 enum AVCodecID codec_id)
247 {
248 uint8_t *user_data;
249 588 int size = bytestream2_get_bytes_left(gb);
250 AVBufferRef *buf_ref, **tmp;
251
252
2/4
✓ Branch 0 taken 588 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 588 times.
588 if (size < 16 || size >= INT_MAX - 1)
253 return AVERROR_INVALIDDATA;
254
255 588 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 588 times.
588 if (!tmp)
257 return AVERROR(ENOMEM);
258 588 h->buf_ref = tmp;
259
260 588 buf_ref = av_buffer_alloc(size + 1);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 588 times.
588 if (!buf_ref)
262 return AVERROR(ENOMEM);
263 588 user_data = buf_ref->data;
264
265 588 bytestream2_get_bufferu(gb, user_data, size);
266 588 user_data[size] = 0;
267 588 buf_ref->size = size;
268 588 h->buf_ref[h->nb_buf_ref++] = buf_ref;
269
270
2/2
✓ Branch 0 taken 568 times.
✓ Branch 1 taken 20 times.
588 if (IS_H264(codec_id)) {
271 int e, build;
272 568 e = sscanf(user_data + 16, "x264 - core %d", &build);
273
4/4
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 425 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 3 times.
568 if (e == 1 && build > 0)
274 140 h->x264_build = build;
275
3/6
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 425 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 143 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
568 if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
276 h->x264_build = 67;
277 }
278
279 588 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 2223 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 1181 times.
✓ Branch 1 taken 588 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 422 times.
2223 switch (type) {
468 1181 case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
469 1181 return decode_registered_user_data(h, gbyte, codec_id, logctx);
470 588 case SEI_TYPE_USER_DATA_UNREGISTERED:
471 588 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 422 default:
489 422 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 34685 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 34685 times.
34979 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 34685 sei->unregistered.nb_buf_ref = 0;
549
550
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 34675 times.
34685 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 34654 times.
34685 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 62 metadata->has_luminance &= sei->mastering_display.min_luminance >= 1 &&
623
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
41 sei->mastering_display.min_luminance <= 50000 &&
624 20 sei->mastering_display.min_luminance <
625
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 sei->mastering_display.max_luminance;
626
627
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)
628 21 av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
629
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (metadata->has_primaries) {
630 21 av_log(avctx, AV_LOG_DEBUG,
631 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
632 21 av_q2d(metadata->display_primaries[0][0]),
633 21 av_q2d(metadata->display_primaries[0][1]),
634 21 av_q2d(metadata->display_primaries[1][0]),
635 21 av_q2d(metadata->display_primaries[1][1]),
636 21 av_q2d(metadata->display_primaries[2][0]),
637 21 av_q2d(metadata->display_primaries[2][1]),
638 21 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
639 }
640
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (metadata->has_luminance) {
641 20 av_log(avctx, AV_LOG_DEBUG,
642 "min_luminance=%f, max_luminance=%f\n",
643 20 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
644 }
645 }
646 }
647
648
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 34681 times.
34685 if (sei->content_light.present) {
649 AVContentLightMetadata *metadata;
650
651 4 ret = ff_decode_content_light_new_ext(avctx, sd, nb_sd, &metadata);
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
653 return ret;
654
655
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (metadata) {
656 4 metadata->MaxCLL = sei->content_light.max_content_light_level;
657 4 metadata->MaxFALL = sei->content_light.max_pic_average_light_level;
658
659 4 av_log(avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
660 4 av_log(avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
661 4 metadata->MaxCLL, metadata->MaxFALL);
662 }
663 }
664
665 34685 return 0;
666 }
667
668 34456 int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
669 enum AVCodecID codec_id,
670 AVCodecContext *avctx, const H2645VUI *vui,
671 unsigned bit_depth_luma, unsigned bit_depth_chroma,
672 int seed)
673 {
674 34456 H2645SEIFramePacking *fp = &sei->frame_packing;
675 int ret;
676
677
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34456 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34456 if (fp->present &&
678 is_frame_packing_type_valid(fp->arrangement_type, codec_id) &&
679 fp->content_interpretation_type > 0 &&
680 fp->content_interpretation_type < 3) {
681 AVStereo3D *stereo = av_stereo3d_create_side_data(frame);
682
683 if (!stereo)
684 return AVERROR(ENOMEM);
685
686 switch (fp->arrangement_type) {
687 #if CONFIG_H264_SEI
688 case SEI_FPA_H264_TYPE_CHECKERBOARD:
689 stereo->type = AV_STEREO3D_CHECKERBOARD;
690 break;
691 case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN:
692 stereo->type = AV_STEREO3D_COLUMNS;
693 break;
694 case SEI_FPA_H264_TYPE_INTERLEAVE_ROW:
695 stereo->type = AV_STEREO3D_LINES;
696 break;
697 #endif
698 case SEI_FPA_TYPE_SIDE_BY_SIDE:
699 if (fp->quincunx_sampling_flag)
700 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
701 else
702 stereo->type = AV_STEREO3D_SIDEBYSIDE;
703 break;
704 case SEI_FPA_TYPE_TOP_BOTTOM:
705 stereo->type = AV_STEREO3D_TOPBOTTOM;
706 break;
707 case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
708 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
709 break;
710 #if CONFIG_H264_SEI
711 case SEI_FPA_H264_TYPE_2D:
712 stereo->type = AV_STEREO3D_2D;
713 break;
714 #endif
715 }
716
717 if (fp->content_interpretation_type == 2)
718 stereo->flags = AV_STEREO3D_FLAG_INVERT;
719
720 if (fp->arrangement_type == SEI_FPA_TYPE_INTERLEAVE_TEMPORAL) {
721 if (fp->current_frame_is_frame0_flag)
722 stereo->view = AV_STEREO3D_VIEW_LEFT;
723 else
724 stereo->view = AV_STEREO3D_VIEW_RIGHT;
725 }
726 }
727
728
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34455 times.
34456 if (sei->display_orientation.present &&
729
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (sei->display_orientation.anticlockwise_rotation ||
730
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 sei->display_orientation.hflip ||
731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 sei->display_orientation.vflip)) {
732 H2645SEIDisplayOrientation *o = &sei->display_orientation;
733 double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
734 AVFrameSideData *rotation = av_frame_new_side_data(frame,
735 AV_FRAME_DATA_DISPLAYMATRIX,
736 sizeof(int32_t) * 9);
737 if (!rotation)
738 return AVERROR(ENOMEM);
739
740 /* av_display_rotation_set() expects the angle in the clockwise
741 * direction, hence the first minus.
742 * The below code applies the flips after the rotation, yet
743 * the H.2645 specs require flipping to be applied first.
744 * Because of R O(phi) = O(-phi) R (where R is flipping around
745 * an arbitatry axis and O(phi) is the proper rotation by phi)
746 * we can create display matrices as desired by negating
747 * the degree once for every flip applied. */
748 angle = -angle * (1 - 2 * !!o->hflip) * (1 - 2 * !!o->vflip);
749 av_display_rotation_set((int32_t *)rotation->data, angle);
750 av_display_matrix_flip((int32_t *)rotation->data,
751 o->hflip, o->vflip);
752 }
753
754
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34455 times.
34456 if (sei->a53_caption.buf_ref) {
755 1 H2645SEIA53Caption *a53 = &sei->a53_caption;
756 1 AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, a53->buf_ref);
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd)
758 av_buffer_unref(&a53->buf_ref);
759 1 a53->buf_ref = NULL;
760
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (avctx)
761 1 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
762 }
763
764 34456 ret = h2645_sei_to_side_data(avctx, sei, &frame->side_data, &frame->nb_side_data);
765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34456 times.
34456 if (ret < 0)
766 return ret;
767
768
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 34135 times.
34456 if (sei->afd.present) {
769 321 AVFrameSideData *sd = av_frame_new_side_data(frame, AV_FRAME_DATA_AFD,
770 sizeof(uint8_t));
771
772
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (sd) {
773 321 *sd->data = sei->afd.active_format_description;
774 321 sei->afd.present = 0;
775 }
776 }
777
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34456 times.
34456 if (sei->film_grain_characteristics.present) {
779 H2645SEIFilmGrainCharacteristics *fgc = &sei->film_grain_characteristics;
780 AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
781 AVFilmGrainH274Params *h274;
782
783 if (!fgp)
784 return AVERROR(ENOMEM);
785
786 fgp->type = AV_FILM_GRAIN_PARAMS_H274;
787 h274 = &fgp->codec.h274;
788
789 fgp->seed = seed;
790 fgp->width = frame->width;
791 fgp->height = frame->height;
792
793 /* H.274 mandates film grain be applied to 4:4:4 frames */
794 fgp->subsampling_x = fgp->subsampling_y = 0;
795
796 h274->model_id = fgc->model_id;
797 if (fgc->separate_colour_description_present_flag) {
798 fgp->bit_depth_luma = fgc->bit_depth_luma;
799 fgp->bit_depth_chroma = fgc->bit_depth_chroma;
800 fgp->color_range = fgc->full_range + 1;
801 fgp->color_primaries = fgc->color_primaries;
802 fgp->color_trc = fgc->transfer_characteristics;
803 fgp->color_space = fgc->matrix_coeffs;
804 } else {
805 fgp->bit_depth_luma = bit_depth_luma;
806 fgp->bit_depth_chroma = bit_depth_chroma;
807 if (vui->video_signal_type_present_flag)
808 fgp->color_range = vui->video_full_range_flag + 1;
809 if (vui->colour_description_present_flag) {
810 fgp->color_primaries = vui->colour_primaries;
811 fgp->color_trc = vui->transfer_characteristics;
812 fgp->color_space = vui->matrix_coeffs;
813 }
814 }
815 h274->blending_mode_id = fgc->blending_mode_id;
816 h274->log2_scale_factor = fgc->log2_scale_factor;
817
818 #if FF_API_H274_FILM_GRAIN_VCS
819 FF_DISABLE_DEPRECATION_WARNINGS
820 h274->bit_depth_luma = fgp->bit_depth_luma;
821 h274->bit_depth_chroma = fgp->bit_depth_chroma;
822 h274->color_range = fgp->color_range;
823 h274->color_primaries = fgp->color_primaries;
824 h274->color_trc = fgp->color_trc;
825 h274->color_space = fgp->color_space;
826 FF_ENABLE_DEPRECATION_WARNINGS
827 #endif
828
829 memcpy(&h274->component_model_present, &fgc->comp_model_present_flag,
830 sizeof(h274->component_model_present));
831 memcpy(&h274->num_intensity_intervals, &fgc->num_intensity_intervals,
832 sizeof(h274->num_intensity_intervals));
833 memcpy(&h274->num_model_values, &fgc->num_model_values,
834 sizeof(h274->num_model_values));
835 memcpy(&h274->intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
836 sizeof(h274->intensity_interval_lower_bound));
837 memcpy(&h274->intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
838 sizeof(h274->intensity_interval_upper_bound));
839 memcpy(&h274->comp_model_value, &fgc->comp_model_value,
840 sizeof(h274->comp_model_value));
841
842 if (IS_H264(codec_id))
843 fgc->present = !!fgc->repetition_period;
844 else
845 fgc->present = fgc->persistence_flag;
846
847 if (avctx)
848 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
849 }
850
851 #if CONFIG_HEVC_SEI
852 34456 ret = ff_aom_attach_film_grain_sets(&sei->aom_film_grain, frame);
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34456 times.
34456 if (ret < 0)
854 return ret;
855 #endif
856
857 34456 return 0;
858 }
859
860 229 int ff_h2645_sei_to_context(AVCodecContext *avctx, H2645SEI *sei)
861 {
862 229 return h2645_sei_to_side_data(avctx, sei, &avctx->decoded_side_data,
863 &avctx->nb_decoded_side_data);
864 }
865
866 73264 void ff_h2645_sei_reset(H2645SEI *s)
867 {
868 73264 av_buffer_unref(&s->a53_caption.buf_ref);
869
870
2/2
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 73264 times.
73558 for (unsigned i = 0; i < s->unregistered.nb_buf_ref; i++)
871 294 av_buffer_unref(&s->unregistered.buf_ref[i]);
872 73264 s->unregistered.nb_buf_ref = 0;
873 73264 av_freep(&s->unregistered.buf_ref);
874 73264 av_buffer_unref(&s->dynamic_hdr_plus.info);
875 73264 av_buffer_unref(&s->dynamic_hdr_vivid.info);
876
877 73264 s->ambient_viewing_environment.present = 0;
878 73264 s->mastering_display.present = 0;
879 73264 s->content_light.present = 0;
880 73264 s->aom_film_grain.enable = 0;
881 73264 }
882