FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_sei.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 216 428 50.5%
Functions: 14 18 77.8%
Branches: 88 226 38.9%

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/pixdesc.h"
34 #include "libavutil/stereo3d.h"
35
36 #include "atsc_a53.h"
37 #include "avcodec.h"
38 #include "dynamic_hdr_vivid.h"
39 #include "get_bits.h"
40 #include "golomb.h"
41 #include "h2645_sei.h"
42
43 #define IS_H264(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_H264 : CONFIG_H264_SEI)
44 #define IS_HEVC(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_HEVC : CONFIG_HEVC_SEI)
45
46 #if CONFIG_HEVC_SEI
47 3 static int decode_registered_user_data_dynamic_hdr_plus(HEVCSEIDynamicHDRPlus *s,
48 GetByteContext *gb)
49 {
50 size_t meta_size;
51 int err;
52 3 AVDynamicHDRPlus *metadata = av_dynamic_hdr_plus_alloc(&meta_size);
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!metadata)
54 return AVERROR(ENOMEM);
55
56 3 err = av_dynamic_hdr_plus_from_t35(metadata, gb->buffer,
57 3 bytestream2_get_bytes_left(gb));
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0) {
59 av_free(metadata);
60 return err;
61 }
62
63 3 av_buffer_unref(&s->info);
64 3 s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0);
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->info) {
66 av_free(metadata);
67 return AVERROR(ENOMEM);
68 }
69
70 3 return 0;
71 }
72
73 3 static int decode_registered_user_data_dynamic_hdr_vivid(HEVCSEIDynamicHDRVivid *s,
74 GetByteContext *gb)
75 {
76 size_t meta_size;
77 int err;
78 3 AVDynamicHDRVivid *metadata = av_dynamic_hdr_vivid_alloc(&meta_size);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!metadata)
80 return AVERROR(ENOMEM);
81
82 3 err = ff_parse_itu_t_t35_to_dynamic_hdr_vivid(metadata,
83 gb->buffer, bytestream2_get_bytes_left(gb));
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0) {
85 av_free(metadata);
86 return err;
87 }
88
89 3 av_buffer_unref(&s->info);
90 3 s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->info) {
92 av_free(metadata);
93 return AVERROR(ENOMEM);
94 }
95
96 3 return 0;
97 }
98 #endif
99
100 1169 static int decode_registered_user_data_afd(H2645SEIAFD *h, GetByteContext *gb)
101 {
102 int flag;
103
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1169 times.
1169 if (bytestream2_get_bytes_left(gb) <= 0)
105 return AVERROR_INVALIDDATA;
106
107 1169 flag = !!(bytestream2_get_byteu(gb) & 0x40); // active_format_flag
108
109
1/2
✓ Branch 0 taken 1169 times.
✗ Branch 1 not taken.
1169 if (flag) {
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1169 times.
1169 if (bytestream2_get_bytes_left(gb) <= 0)
111 return AVERROR_INVALIDDATA;
112 1169 h->active_format_description = bytestream2_get_byteu(gb) & 0xF;
113 1169 h->present = 1;
114 }
115
116 1169 return 0;
117 }
118
119 6 static int decode_registered_user_data_closed_caption(H2645SEIA53Caption *h,
120 GetByteContext *gb)
121 {
122 6 return ff_parse_a53_cc(&h->buf_ref, gb->buffer,
123 bytestream2_get_bytes_left(gb));
124 }
125
126 1181 static int decode_registered_user_data(H2645SEI *h, GetByteContext *gb,
127 enum AVCodecID codec_id, void *logctx)
128 {
129 int country_code, provider_code;
130
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1181 times.
1181 if (bytestream2_get_bytes_left(gb) < 3)
132 return AVERROR_INVALIDDATA;
133
134 1181 country_code = bytestream2_get_byteu(gb); // itu_t_t35_country_code
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1181 times.
1181 if (country_code == 0xFF) {
136 if (bytestream2_get_bytes_left(gb) < 3)
137 return AVERROR_INVALIDDATA;
138
139 bytestream2_skipu(gb, 1); // itu_t_t35_country_code_extension_byte
140 }
141
142
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 != 0xB5 && country_code != 0x26) { // usa_country_code and cn_country_code
143 av_log(logctx, AV_LOG_VERBOSE,
144 "Unsupported User Data Registered ITU-T T35 SEI message (country_code = %d)\n",
145 country_code);
146 return 0;
147 }
148
149 /* itu_t_t35_payload_byte follows */
150 1181 provider_code = bytestream2_get_be16u(gb);
151
152
3/4
✓ Branch 0 taken 1175 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
1181 switch (provider_code) {
153 1175 case 0x31: { // atsc_provider_code
154 uint32_t user_identifier;
155
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1175 times.
1175 if (bytestream2_get_bytes_left(gb) < 4)
157 return AVERROR_INVALIDDATA;
158
159 1175 user_identifier = bytestream2_get_be32u(gb);
160 switch (user_identifier) {
161 1169 case MKBETAG('D', 'T', 'G', '1'): // afd_data
162 1169 return decode_registered_user_data_afd(&h->afd, gb);
163 6 case MKBETAG('G', 'A', '9', '4'): // closed captions
164 6 return decode_registered_user_data_closed_caption(&h->a53_caption, gb);
165 default:
166 av_log(logctx, AV_LOG_VERBOSE,
167 "Unsupported User Data Registered ITU-T T35 SEI message (atsc user_identifier = 0x%04x)\n",
168 user_identifier);
169 break;
170 }
171 break;
172 }
173 #if CONFIG_HEVC_SEI
174 3 case 0x04: { // cuva_provider_code
175 3 const uint16_t cuva_provider_oriented_code = 0x0005;
176 uint16_t provider_oriented_code;
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!IS_HEVC(codec_id))
179 goto unsupported_provider_code;
180
181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(gb) < 2)
182 return AVERROR_INVALIDDATA;
183
184 3 provider_oriented_code = bytestream2_get_be16u(gb);
185
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (provider_oriented_code == cuva_provider_oriented_code) {
186 3 return decode_registered_user_data_dynamic_hdr_vivid(&h->dynamic_hdr_vivid, gb);
187 }
188 break;
189 }
190 3 case 0x3C: { // smpte_provider_code
191 // A/341 Amendment - 2094-40
192 3 const uint16_t smpte2094_40_provider_oriented_code = 0x0001;
193 3 const uint8_t smpte2094_40_application_identifier = 0x04;
194 uint16_t provider_oriented_code;
195 uint8_t application_identifier;
196
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!IS_HEVC(codec_id))
198 goto unsupported_provider_code;
199
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(gb) < 3)
201 return AVERROR_INVALIDDATA;
202
203 3 provider_oriented_code = bytestream2_get_be16u(gb);
204 3 application_identifier = bytestream2_get_byteu(gb);
205
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 &&
206 application_identifier == smpte2094_40_application_identifier) {
207 3 return decode_registered_user_data_dynamic_hdr_plus(&h->dynamic_hdr_plus, gb);
208 }
209 break;
210 }
211 unsupported_provider_code:
212 #endif
213 default:
214 av_log(logctx, AV_LOG_VERBOSE,
215 "Unsupported User Data Registered ITU-T T35 SEI message (provider_code = %d)\n",
216 provider_code);
217 break;
218 }
219
220 return 0;
221 }
222
223 592 static int decode_unregistered_user_data(H2645SEIUnregistered *h,
224 GetByteContext *gb,
225 enum AVCodecID codec_id)
226 {
227 uint8_t *user_data;
228 592 int size = bytestream2_get_bytes_left(gb);
229 AVBufferRef *buf_ref, **tmp;
230
231
2/4
✓ Branch 0 taken 592 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 592 times.
592 if (size < 16 || size >= INT_MAX - 1)
232 return AVERROR_INVALIDDATA;
233
234 592 tmp = av_realloc_array(h->buf_ref, h->nb_buf_ref + 1, sizeof(*h->buf_ref));
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
592 if (!tmp)
236 return AVERROR(ENOMEM);
237 592 h->buf_ref = tmp;
238
239 592 buf_ref = av_buffer_alloc(size + 1);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
592 if (!buf_ref)
241 return AVERROR(ENOMEM);
242 592 user_data = buf_ref->data;
243
244 592 bytestream2_get_bufferu(gb, user_data, size);
245 592 user_data[size] = 0;
246 592 buf_ref->size = size;
247 592 h->buf_ref[h->nb_buf_ref++] = buf_ref;
248
249
2/2
✓ Branch 0 taken 575 times.
✓ Branch 1 taken 17 times.
592 if (IS_H264(codec_id)) {
250 int e, build;
251 575 e = sscanf(user_data + 16, "x264 - core %d", &build);
252
4/4
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 432 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 3 times.
575 if (e == 1 && build > 0)
253 140 h->x264_build = build;
254
3/6
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 143 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
575 if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
255 h->x264_build = 67;
256 }
257
258 592 return 0;
259 }
260
261 2 static int decode_display_orientation(H2645SEIDisplayOrientation *h,
262 GetBitContext *gb)
263 {
264 2 h->present = !get_bits1(gb); // display_orientation_cancel_flag
265
266
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (h->present) {
267 2 h->hflip = get_bits1(gb); // hor_flip
268 2 h->vflip = get_bits1(gb); // ver_flip
269
270 2 h->anticlockwise_rotation = get_bits(gb, 16);
271 // This is followed by display_orientation_repetition_period
272 // and display_orientation_extension_flag for H.264
273 // and by display_orientation_persistence_flag for HEVC.
274 }
275
276 2 return 0;
277 }
278
279 static int decode_frame_packing_arrangement(H2645SEIFramePacking *h,
280 GetBitContext *gb,
281 enum AVCodecID codec_id)
282 {
283 h->arrangement_id = get_ue_golomb_long(gb);
284 h->arrangement_cancel_flag = get_bits1(gb);
285 h->present = !h->arrangement_cancel_flag;
286
287 if (h->present) {
288 h->arrangement_type = get_bits(gb, 7);
289 h->quincunx_sampling_flag = get_bits1(gb);
290 h->content_interpretation_type = get_bits(gb, 6);
291
292 // spatial_flipping_flag, frame0_flipped_flag, field_views_flag
293 skip_bits(gb, 3);
294 h->current_frame_is_frame0_flag = get_bits1(gb);
295 // frame0_self_contained_flag, frame1_self_contained_flag
296 skip_bits(gb, 2);
297
298 if (!h->quincunx_sampling_flag && h->arrangement_type != 5)
299 skip_bits(gb, 16); // frame[01]_grid_position_[xy]
300 skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
301 if (IS_H264(codec_id))
302 h->arrangement_repetition_period = get_ue_golomb_long(gb);
303 else
304 skip_bits1(gb); // frame_packing_arrangement_persistence_flag
305 }
306 // H.264: frame_packing_arrangement_extension_flag,
307 // HEVC: upsampled_aspect_ratio_flag
308 skip_bits1(gb);
309
310 return 0;
311 }
312
313 static int decode_alternative_transfer(H2645SEIAlternativeTransfer *s,
314 GetByteContext *gb)
315 {
316 if (bytestream2_get_bytes_left(gb) < 1)
317 return AVERROR_INVALIDDATA;
318
319 s->present = 1;
320 s->preferred_transfer_characteristics = bytestream2_get_byteu(gb);
321
322 return 0;
323 }
324
325 9 static int decode_ambient_viewing_environment(H2645SEIAmbientViewingEnvironment *s,
326 GetByteContext *gb)
327 {
328 static const uint16_t max_ambient_light_value = 50000;
329
330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (bytestream2_get_bytes_left(gb) < 8)
331 return AVERROR_INVALIDDATA;
332
333 9 s->ambient_illuminance = bytestream2_get_be32u(gb);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!s->ambient_illuminance)
335 return AVERROR_INVALIDDATA;
336
337 9 s->ambient_light_x = bytestream2_get_be16u(gb);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->ambient_light_x > max_ambient_light_value)
339 return AVERROR_INVALIDDATA;
340
341 9 s->ambient_light_y = bytestream2_get_be16u(gb);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->ambient_light_y > max_ambient_light_value)
343 return AVERROR_INVALIDDATA;
344
345 9 s->present = 1;
346
347 9 return 0;
348 }
349
350 static int decode_film_grain_characteristics(H2645SEIFilmGrainCharacteristics *h,
351 enum AVCodecID codec_id, GetBitContext *gb)
352 {
353 h->present = !get_bits1(gb); // film_grain_characteristics_cancel_flag
354
355 if (h->present) {
356 memset(h, 0, sizeof(*h));
357 h->model_id = get_bits(gb, 2);
358 h->separate_colour_description_present_flag = get_bits1(gb);
359 if (h->separate_colour_description_present_flag) {
360 h->bit_depth_luma = get_bits(gb, 3) + 8;
361 h->bit_depth_chroma = get_bits(gb, 3) + 8;
362 h->full_range = get_bits1(gb);
363 h->color_primaries = get_bits(gb, 8);
364 h->transfer_characteristics = get_bits(gb, 8);
365 h->matrix_coeffs = get_bits(gb, 8);
366 }
367 h->blending_mode_id = get_bits(gb, 2);
368 h->log2_scale_factor = get_bits(gb, 4);
369 for (int c = 0; c < 3; c++)
370 h->comp_model_present_flag[c] = get_bits1(gb);
371 for (int c = 0; c < 3; c++) {
372 if (h->comp_model_present_flag[c]) {
373 h->num_intensity_intervals[c] = get_bits(gb, 8) + 1;
374 h->num_model_values[c] = get_bits(gb, 3) + 1;
375 if (h->num_model_values[c] > 6)
376 return AVERROR_INVALIDDATA;
377 for (int i = 0; i < h->num_intensity_intervals[c]; i++) {
378 h->intensity_interval_lower_bound[c][i] = get_bits(gb, 8);
379 h->intensity_interval_upper_bound[c][i] = get_bits(gb, 8);
380 for (int j = 0; j < h->num_model_values[c]; j++)
381 h->comp_model_value[c][i][j] = get_se_golomb_long(gb);
382 }
383 }
384 }
385 if (IS_HEVC(codec_id))
386 h->persistence_flag = get_bits1(gb);
387 else
388 h->repetition_period = get_ue_golomb_long(gb);
389
390 h->present = 1;
391 }
392
393 return 0;
394 }
395
396 8 static int decode_nal_sei_mastering_display_info(H2645SEIMasteringDisplay *s,
397 GetByteContext *gb)
398 {
399 int i;
400
401
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (bytestream2_get_bytes_left(gb) < 24)
402 return AVERROR_INVALIDDATA;
403
404 // Mastering primaries
405
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 for (i = 0; i < 3; i++) {
406 24 s->display_primaries[i][0] = bytestream2_get_be16u(gb);
407 24 s->display_primaries[i][1] = bytestream2_get_be16u(gb);
408 }
409 // White point (x, y)
410 8 s->white_point[0] = bytestream2_get_be16u(gb);
411 8 s->white_point[1] = bytestream2_get_be16u(gb);
412
413 // Max and min luminance of mastering display
414 8 s->max_luminance = bytestream2_get_be32u(gb);
415 8 s->min_luminance = bytestream2_get_be32u(gb);
416
417 // As this SEI message comes before the first frame that references it,
418 // initialize the flag to 2 and decrement on IRAP access unit so it
419 // persists for the coded video sequence (e.g., between two IRAPs)
420 8 s->present = 2;
421
422 8 return 0;
423 }
424
425 6 static int decode_nal_sei_content_light_info(H2645SEIContentLight *s,
426 GetByteContext *gb)
427 {
428
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_bytes_left(gb) < 4)
429 return AVERROR_INVALIDDATA;
430
431 // Max and average light levels
432 6 s->max_content_light_level = bytestream2_get_be16u(gb);
433 6 s->max_pic_average_light_level = bytestream2_get_be16u(gb);
434 // As this SEI message comes before the first frame that references it,
435 // initialize the flag to 2 and decrement on IRAP access unit so it
436 // persists for the coded video sequence (e.g., between two IRAPs)
437 6 s->present = 2;
438
439 6 return 0;
440 }
441
442 2220 int ff_h2645_sei_message_decode(H2645SEI *h, enum SEIType type,
443 enum AVCodecID codec_id, GetBitContext *gb,
444 GetByteContext *gbyte, void *logctx)
445 {
446
7/10
✓ Branch 0 taken 1181 times.
✓ Branch 1 taken 592 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 8 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 422 times.
2220 switch (type) {
447 1181 case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
448 1181 return decode_registered_user_data(h, gbyte, codec_id, logctx);
449 592 case SEI_TYPE_USER_DATA_UNREGISTERED:
450 592 return decode_unregistered_user_data(&h->unregistered, gbyte, codec_id);
451 2 case SEI_TYPE_DISPLAY_ORIENTATION:
452 2 return decode_display_orientation(&h->display_orientation, gb);
453 case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
454 return decode_film_grain_characteristics(&h->film_grain_characteristics, codec_id, gb);
455 case SEI_TYPE_FRAME_PACKING_ARRANGEMENT:
456 return decode_frame_packing_arrangement(&h->frame_packing, gb, codec_id);
457 case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
458 return decode_alternative_transfer(&h->alternative_transfer, gbyte);
459 9 case SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT:
460 9 return decode_ambient_viewing_environment(&h->ambient_viewing_environment,
461 gbyte);
462 8 case SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
463 8 return decode_nal_sei_mastering_display_info(&h->mastering_display,
464 gbyte);
465 6 case SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
466 6 return decode_nal_sei_content_light_info(&h->content_light, gbyte);
467 422 default:
468 422 return FF_H2645_SEI_MESSAGE_UNHANDLED;
469 }
470 }
471
472 94 int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src)
473 {
474 94 int ret = av_buffer_replace(&dst->a53_caption.buf_ref,
475 94 src->a53_caption.buf_ref);
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (ret < 0)
477 return ret;
478
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 for (unsigned i = 0; i < dst->unregistered.nb_buf_ref; i++)
480 av_buffer_unref(&dst->unregistered.buf_ref[i]);
481 94 dst->unregistered.nb_buf_ref = 0;
482
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (src->unregistered.nb_buf_ref) {
484 ret = av_reallocp_array(&dst->unregistered.buf_ref,
485 src->unregistered.nb_buf_ref,
486 sizeof(*dst->unregistered.buf_ref));
487 if (ret < 0)
488 return ret;
489
490 for (unsigned i = 0; i < src->unregistered.nb_buf_ref; i++) {
491 dst->unregistered.buf_ref[i] = av_buffer_ref(src->unregistered.buf_ref[i]);
492 if (!dst->unregistered.buf_ref[i])
493 return AVERROR(ENOMEM);
494 dst->unregistered.nb_buf_ref++;
495 }
496 }
497
498 94 return 0;
499 }
500
501 static int is_frame_packing_type_valid(SEIFpaType type, enum AVCodecID codec_id)
502 {
503 if (IS_H264(codec_id))
504 return type <= SEI_FPA_H264_TYPE_2D &&
505 type >= SEI_FPA_H264_TYPE_CHECKERBOARD;
506 else
507 return type <= SEI_FPA_TYPE_INTERLEAVE_TEMPORAL &&
508 type >= SEI_FPA_TYPE_SIDE_BY_SIDE;
509 }
510
511 34439 int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
512 enum AVCodecID codec_id,
513 AVCodecContext *avctx, const H2645VUI *vui,
514 unsigned bit_depth_luma, unsigned bit_depth_chroma,
515 int seed)
516 {
517 34439 H2645SEIFramePacking *fp = &sei->frame_packing;
518
519
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34439 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34439 if (fp->present &&
520 is_frame_packing_type_valid(fp->arrangement_type, codec_id) &&
521 fp->content_interpretation_type > 0 &&
522 fp->content_interpretation_type < 3) {
523 AVStereo3D *stereo = av_stereo3d_create_side_data(frame);
524
525 if (!stereo)
526 return AVERROR(ENOMEM);
527
528 switch (fp->arrangement_type) {
529 #if CONFIG_H264_SEI
530 case SEI_FPA_H264_TYPE_CHECKERBOARD:
531 stereo->type = AV_STEREO3D_CHECKERBOARD;
532 break;
533 case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN:
534 stereo->type = AV_STEREO3D_COLUMNS;
535 break;
536 case SEI_FPA_H264_TYPE_INTERLEAVE_ROW:
537 stereo->type = AV_STEREO3D_LINES;
538 break;
539 #endif
540 case SEI_FPA_TYPE_SIDE_BY_SIDE:
541 if (fp->quincunx_sampling_flag)
542 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
543 else
544 stereo->type = AV_STEREO3D_SIDEBYSIDE;
545 break;
546 case SEI_FPA_TYPE_TOP_BOTTOM:
547 stereo->type = AV_STEREO3D_TOPBOTTOM;
548 break;
549 case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
550 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
551 break;
552 #if CONFIG_H264_SEI
553 case SEI_FPA_H264_TYPE_2D:
554 stereo->type = AV_STEREO3D_2D;
555 break;
556 #endif
557 }
558
559 if (fp->content_interpretation_type == 2)
560 stereo->flags = AV_STEREO3D_FLAG_INVERT;
561
562 if (fp->arrangement_type == SEI_FPA_TYPE_INTERLEAVE_TEMPORAL) {
563 if (fp->current_frame_is_frame0_flag)
564 stereo->view = AV_STEREO3D_VIEW_LEFT;
565 else
566 stereo->view = AV_STEREO3D_VIEW_RIGHT;
567 }
568 }
569
570
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34438 times.
34439 if (sei->display_orientation.present &&
571
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (sei->display_orientation.anticlockwise_rotation ||
572
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 sei->display_orientation.hflip ||
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 sei->display_orientation.vflip)) {
574 H2645SEIDisplayOrientation *o = &sei->display_orientation;
575 double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
576 AVFrameSideData *rotation = av_frame_new_side_data(frame,
577 AV_FRAME_DATA_DISPLAYMATRIX,
578 sizeof(int32_t) * 9);
579 if (!rotation)
580 return AVERROR(ENOMEM);
581
582 /* av_display_rotation_set() expects the angle in the clockwise
583 * direction, hence the first minus.
584 * The below code applies the flips after the rotation, yet
585 * the H.2645 specs require flipping to be applied first.
586 * Because of R O(phi) = O(-phi) R (where R is flipping around
587 * an arbitatry axis and O(phi) is the proper rotation by phi)
588 * we can create display matrices as desired by negating
589 * the degree once for every flip applied. */
590 angle = -angle * (1 - 2 * !!o->hflip) * (1 - 2 * !!o->vflip);
591 av_display_rotation_set((int32_t *)rotation->data, angle);
592 av_display_matrix_flip((int32_t *)rotation->data,
593 o->hflip, o->vflip);
594 }
595
596
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34438 times.
34439 if (sei->a53_caption.buf_ref) {
597 1 H2645SEIA53Caption *a53 = &sei->a53_caption;
598 1 AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, a53->buf_ref);
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd)
600 av_buffer_unref(&a53->buf_ref);
601 1 a53->buf_ref = NULL;
602
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (avctx)
603 1 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
604 }
605
606
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 34439 times.
34729 for (unsigned i = 0; i < sei->unregistered.nb_buf_ref; i++) {
607 290 H2645SEIUnregistered *unreg = &sei->unregistered;
608
609
1/2
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
290 if (unreg->buf_ref[i]) {
610 290 AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame,
611 AV_FRAME_DATA_SEI_UNREGISTERED,
612 290 unreg->buf_ref[i]);
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (!sd)
614 av_buffer_unref(&unreg->buf_ref[i]);
615 290 unreg->buf_ref[i] = NULL;
616 }
617 }
618 34439 sei->unregistered.nb_buf_ref = 0;
619
620
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 34118 times.
34439 if (sei->afd.present) {
621 321 AVFrameSideData *sd = av_frame_new_side_data(frame, AV_FRAME_DATA_AFD,
622 sizeof(uint8_t));
623
624
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (sd) {
625 321 *sd->data = sei->afd.active_format_description;
626 321 sei->afd.present = 0;
627 }
628 }
629
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34439 times.
34439 if (sei->film_grain_characteristics.present) {
631 H2645SEIFilmGrainCharacteristics *fgc = &sei->film_grain_characteristics;
632 AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
633 AVFilmGrainH274Params *h274;
634
635 if (!fgp)
636 return AVERROR(ENOMEM);
637
638 fgp->type = AV_FILM_GRAIN_PARAMS_H274;
639 h274 = &fgp->codec.h274;
640
641 fgp->seed = seed;
642
643 h274->model_id = fgc->model_id;
644 if (fgc->separate_colour_description_present_flag) {
645 h274->bit_depth_luma = fgc->bit_depth_luma;
646 h274->bit_depth_chroma = fgc->bit_depth_chroma;
647 h274->color_range = fgc->full_range + 1;
648 h274->color_primaries = fgc->color_primaries;
649 h274->color_trc = fgc->transfer_characteristics;
650 h274->color_space = fgc->matrix_coeffs;
651 } else {
652 h274->bit_depth_luma = bit_depth_luma;
653 h274->bit_depth_chroma = bit_depth_chroma;
654 if (vui->video_signal_type_present_flag)
655 h274->color_range = vui->video_full_range_flag + 1;
656 else
657 h274->color_range = AVCOL_RANGE_UNSPECIFIED;
658 if (vui->colour_description_present_flag) {
659 h274->color_primaries = vui->colour_primaries;
660 h274->color_trc = vui->transfer_characteristics;
661 h274->color_space = vui->matrix_coeffs;
662 } else {
663 h274->color_primaries = AVCOL_PRI_UNSPECIFIED;
664 h274->color_trc = AVCOL_TRC_UNSPECIFIED;
665 h274->color_space = AVCOL_SPC_UNSPECIFIED;
666 }
667 }
668 h274->blending_mode_id = fgc->blending_mode_id;
669 h274->log2_scale_factor = fgc->log2_scale_factor;
670
671 memcpy(&h274->component_model_present, &fgc->comp_model_present_flag,
672 sizeof(h274->component_model_present));
673 memcpy(&h274->num_intensity_intervals, &fgc->num_intensity_intervals,
674 sizeof(h274->num_intensity_intervals));
675 memcpy(&h274->num_model_values, &fgc->num_model_values,
676 sizeof(h274->num_model_values));
677 memcpy(&h274->intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
678 sizeof(h274->intensity_interval_lower_bound));
679 memcpy(&h274->intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
680 sizeof(h274->intensity_interval_upper_bound));
681 memcpy(&h274->comp_model_value, &fgc->comp_model_value,
682 sizeof(h274->comp_model_value));
683
684 if (IS_H264(codec_id))
685 fgc->present = !!fgc->repetition_period;
686 else
687 fgc->present = fgc->persistence_flag;
688
689 if (avctx)
690 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
691 }
692
693
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 34437 times.
34439 if (sei->ambient_viewing_environment.present) {
694 2 H2645SEIAmbientViewingEnvironment *env =
695 &sei->ambient_viewing_environment;
696
697 AVAmbientViewingEnvironment *dst_env =
698 2 av_ambient_viewing_environment_create_side_data(frame);
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!dst_env)
700 return AVERROR(ENOMEM);
701
702 2 dst_env->ambient_illuminance = av_make_q(env->ambient_illuminance, 10000);
703 2 dst_env->ambient_light_x = av_make_q(env->ambient_light_x, 50000);
704 2 dst_env->ambient_light_y = av_make_q(env->ambient_light_y, 50000);
705 }
706
707
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 34434 times.
34439 if (sei->mastering_display.present) {
708 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
709 5 const int mapping[3] = {2, 0, 1};
710 5 const int chroma_den = 50000;
711 5 const int luma_den = 10000;
712 int i;
713 AVMasteringDisplayMetadata *metadata =
714 5 av_mastering_display_metadata_create_side_data(frame);
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!metadata)
716 return AVERROR(ENOMEM);
717
718
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 for (i = 0; i < 3; i++) {
719 15 const int j = mapping[i];
720 15 metadata->display_primaries[i][0].num = sei->mastering_display.display_primaries[j][0];
721 15 metadata->display_primaries[i][0].den = chroma_den;
722 15 metadata->display_primaries[i][1].num = sei->mastering_display.display_primaries[j][1];
723 15 metadata->display_primaries[i][1].den = chroma_den;
724 }
725 5 metadata->white_point[0].num = sei->mastering_display.white_point[0];
726 5 metadata->white_point[0].den = chroma_den;
727 5 metadata->white_point[1].num = sei->mastering_display.white_point[1];
728 5 metadata->white_point[1].den = chroma_den;
729
730 5 metadata->max_luminance.num = sei->mastering_display.max_luminance;
731 5 metadata->max_luminance.den = luma_den;
732 5 metadata->min_luminance.num = sei->mastering_display.min_luminance;
733 5 metadata->min_luminance.den = luma_den;
734 5 metadata->has_luminance = 1;
735 5 metadata->has_primaries = 1;
736
737 5 av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
738 5 av_log(avctx, AV_LOG_DEBUG,
739 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
740 av_q2d(metadata->display_primaries[0][0]),
741 av_q2d(metadata->display_primaries[0][1]),
742 av_q2d(metadata->display_primaries[1][0]),
743 av_q2d(metadata->display_primaries[1][1]),
744 av_q2d(metadata->display_primaries[2][0]),
745 av_q2d(metadata->display_primaries[2][1]),
746 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
747 5 av_log(avctx, AV_LOG_DEBUG,
748 "min_luminance=%f, max_luminance=%f\n",
749 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
750 }
751
752
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 34435 times.
34439 if (sei->content_light.present) {
753 AVContentLightMetadata *metadata =
754 4 av_content_light_metadata_create_side_data(frame);
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!metadata)
756 return AVERROR(ENOMEM);
757 4 metadata->MaxCLL = sei->content_light.max_content_light_level;
758 4 metadata->MaxFALL = sei->content_light.max_pic_average_light_level;
759
760 4 av_log(avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
761 4 av_log(avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
762 metadata->MaxCLL, metadata->MaxFALL);
763 }
764
765 34439 return 0;
766 }
767
768 73205 void ff_h2645_sei_reset(H2645SEI *s)
769 {
770 73205 av_buffer_unref(&s->a53_caption.buf_ref);
771
772
2/2
✓ Branch 0 taken 302 times.
✓ Branch 1 taken 73205 times.
73507 for (unsigned i = 0; i < s->unregistered.nb_buf_ref; i++)
773 302 av_buffer_unref(&s->unregistered.buf_ref[i]);
774 73205 s->unregistered.nb_buf_ref = 0;
775 73205 av_freep(&s->unregistered.buf_ref);
776 73205 av_buffer_unref(&s->dynamic_hdr_plus.info);
777 73205 av_buffer_unref(&s->dynamic_hdr_vivid.info);
778
779 73205 s->ambient_viewing_environment.present = 0;
780 73205 s->mastering_display.present = 0;
781 73205 s->content_light.present = 0;
782 73205 }
783